package hdmistat

import (
	"fmt"
	"os/exec"

	"github.com/spf13/cobra"
)

// newStatusCmd creates the status command
func (c *CLI) newStatusCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "status",
		Short: "Show hdmistat daemon status",
		Long:  `Show the current status of the hdmistat systemd service.`,
		Run:   c.runStatus,
	}
}

func (c *CLI) runStatus(_ *cobra.Command, _ []string) {
	// Check systemd service status
	out, err := exec.Command("systemctl", "status", "hdmistat.service", "--no-pager").Output()
	if err != nil {
		// Service might not be installed
		if exitErr, ok := err.(*exec.ExitError); ok {
			fmt.Printf("hdmistat service status:\n%s", exitErr.Stderr)
			const systemdServiceNotFoundCode = 4
			if exitErr.ExitCode() == systemdServiceNotFoundCode {
				fmt.Println("\nhdmistat service is not installed. Run 'sudo hdmistat install' to install it.")
			}
		} else {
			c.log.Error("checking service status", "error", err)
		}
		return
	}

	fmt.Printf("hdmistat service status:\n%s", out)
}
