package cmd

import (
	"fmt"
	"path/filepath"

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

func makeDebs() *cobra.Command {
	cmd := &cobra.Command{
		Use:     "debs",
		Aliases: []string{"deb"},
		Short:   "Build Debian packages",
		Long:    `Build all required Debian packages for the project.`,
		RunE: func(cmd *cobra.Command, args []string) error {
			fmt.Println("Building Debian packages...")
			var (
				tarURL = "https://artifactory.yizhisec.com:443/artifactory/filestore/hsv3/deb/docker.tar.gz"
				binDir = filepath.Join(opt.Cfg.Make.Dir, "dependency", "deb")
			)

			logger.Info("开始准备 deb(docker) 文件...")
			logger.Debug("下载地址: %s", tarURL)
			logger.Debug("目标目录: %s", binDir)

			logger.Info("正在下载二进制文件...")

			if err := archiver.DownloadAndExtract(
				cmd.Context(),
				tarURL,
				binDir,
				archiver.WithInsecureSkipVerify(),
				archiver.WithGzipCompression(true),
			); err != nil {
				logger.Info("❌ 下载并解压 deb(docker) 文件失败")
				logger.Debug("下载并解压 deb(docker) 文件失败: %v", err)
				return err
			}

			logger.Info("✅ 成功下载并解压 deb(docker) 文件")
			return nil
		},
	}

	return cmd
}
