package main

import (
	"os"
	"qvm/internal/config"
	"qvm/internal/logging"
	"qvm/internal/vm"
	"qvm/internal/workspace"

	"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "Start the VM",
	Long: `Start the QVM virtual machine.

Creates base image and overlay if they don't exist.
Mounts all registered workspaces and cache directories.
Waits for SSH to become available.`,
	Run: func(cmd *cobra.Command, args []string) {
		cfg, err := config.Load()
		if err != nil {
			logging.Error(err.Error())
			os.Exit(1)
		}

		regResult := workspace.Load(config.WorkspacesFile)
		if regResult.IsError() {
			logging.Error(regResult.Error().Error())
			os.Exit(1)
		}
		reg := regResult.MustGet()

		logging.Info("Starting VM...")

		result := vm.Start(cfg, reg)
		if result.IsError() {
			logging.Error(result.Error().Error())
			os.Exit(1)
		}

		logging.Info("VM started successfully")
	},
}
