package cmd

import (
	"os"

	"gitea.loveuer.com/yizhisec/pkg3/logger"
	"github.com/spf13/cobra"
	"yizhisec.com/hsv2/forge/internal/controller/maker"
	"yizhisec.com/hsv2/forge/internal/opt"
)

func makeCmd() *cobra.Command {

	_cmd := &cobra.Command{
		Use:   "make",
		Short: "Build the project",
		Long:  `Build the project using the specified configuration.`,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			var (
				err error
			)

			if opt.Cfg.Debug {
				logger.SetLogLevel(logger.LogLevelDebug)
				logger.Warn("Running in debug mode")
			}

			if opt.Cfg.Make.DisableDependencyCheck {
				logger.Info("Dependency check disabled")
				return nil
			}

			mk := maker.NewMaker()
			if err = mk.DependencyCheck(cmd.Context()); err != nil {
				return err
			}

			if err = os.MkdirAll(opt.Cfg.Make.Dir, 0755); err != nil {
				return err
			}

			logger.Debug("Running make prerun success")

			return nil
		},
	}

	_cmd.PersistentFlags().BoolVar(&opt.Cfg.Make.DisableDependencyCheck, "disable-dependency-check", false, "Disable dependency check")
	_cmd.PersistentFlags().StringVar(&opt.Cfg.Make.Dir, "dir", "/root/hsv2-installation", "make base directory")

	_cmd.AddCommand(
		makeImages(),
		makeBinaries(),
		makeDebs(),
		makeFlannel(),
		makeLonghorn(),
		makeMysql(),
		makeRedis(),
		makeES(),
		makeEMQX(),
		makeYosguard(),
		makeLessDNS(), // hs-net dependency
		makeHSNet(),
	)

	return _cmd
}
