package main

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

	"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
	Use:   "status",
	Short: "Show VM status",
	Long: `Display VM state, SSH port, and mounted workspaces.

Shows current running state, PID, SSH connection info,
and list of registered workspaces.`,
	Run: func(cmd *cobra.Command, args []string) {
		statusResult := vm.Status()
		if statusResult.IsError() {
			logging.Error(statusResult.Error().Error())
			os.Exit(1)
		}
		status := statusResult.MustGet()

		if status.Running {
			fmt.Printf("VM Status: Running\n")
			fmt.Printf("PID: %d\n", status.PID)
			fmt.Printf("SSH Port: %d\n", status.SSHPort)
		} else {
			fmt.Printf("VM Status: Stopped\n")
		}

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

		workspaces := reg.List()
		if len(workspaces) > 0 {
			fmt.Printf("\nMounted Workspaces:\n")
			for _, ws := range workspaces {
				fmt.Printf("  %s -> %s\n", ws.Hash, ws.HostPath)
			}
		} else {
			fmt.Printf("\nMounted Workspaces:\n  (none)\n")
		}
	},
}
