package initcmd

import (
	"fmt"

	"sunhpc/internal/auth"
	"sunhpc/internal/config"
	"sunhpc/internal/log"
	"sunhpc/internal/service"

	"github.com/spf13/cobra"
)

var serviceCmd = &cobra.Command{
	Use:   "service",
	Short: "根据配置文件初始化服务",
	Long: `读取 /etc/sunhpc/services.yaml 并部署/配置相关服务。
支持 HTTPD、TFTPD、DHCPD 等。`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if err := auth.RequireRoot(); err != nil {
			return err
		}

		svcCfg, err := config.LoadServices()
		if err != nil {
			return fmt.Errorf("加载 services.yaml 失败: %v", err)
		}

		log.Info("开始部署服务...")
		if err := service.Deploy(svcCfg); err != nil {
			return fmt.Errorf("服务部署失败: %v", err)
		}

		log.Info("服务初始化完成")
		return nil
	},
}
