// Package hdmistat provides the CLI commands for the hdmistat application
package hdmistat

import (
	"log/slog"
	"os"

	"github.com/spf13/cobra"
)

// CLI represents the command line interface
type CLI struct {
	log     *slog.Logger
	rootCmd *cobra.Command
}

// NewCLI creates a new CLI instance
func NewCLI() *CLI {
	log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	}))

	cli := &CLI{
		log: log,
		rootCmd: &cobra.Command{
			Use:   "hdmistat",
			Short: "System statistics display for Linux framebuffers",
			Long:  `hdmistat displays beautiful system statistics on Linux framebuffers using IBM Plex Mono font.`,
		},
	}

	// Add commands
	cli.addCommands()

	return cli
}

// addCommands adds all subcommands to the root command
func (c *CLI) addCommands() {
	c.rootCmd.AddCommand(c.newDaemonCmd())
	c.rootCmd.AddCommand(c.newInstallCmd())
	c.rootCmd.AddCommand(c.newStatusCmd())
	c.rootCmd.AddCommand(c.newInfoCmd())
}

// Execute runs the CLI
func (c *CLI) Execute() error {
	return c.rootCmd.Execute()
}

// CLIEntry is the main entry point for the CLI
func CLIEntry() {
	cli := NewCLI()
	if err := cli.Execute(); err != nil {
		cli.log.Error("command failed", "error", err)
		os.Exit(1)
	}
}
