package maker

import (
	"context"
	"os"
	"path/filepath"

	"gitea.loveuer.com/yizhisec/pkg3/logger"
	"yizhisec.com/hsv2/forge/internal/opt"
	"yizhisec.com/hsv2/forge/pkg/resource"
)

func (m *maker) LessDNS(ctx context.Context) error {
	var (
		err      error
		location = filepath.Join(opt.Cfg.Make.Dir, "dependency", "less-dns")
	)

	logger.Info("☑️ maker.LessDNS: 开始构建 less-dns...")
	logger.Debug("☑️ maker.LessDNS: 开始构建 less-dns...")
	logger.Debug("☑️ maker.LessDNS: 创建目录 %s", location)
	if err = os.MkdirAll(location, 0755); err != nil {
		logger.Debug("❌ maker.LessDNS: 创建目录失败: %v", err)
		return err
	}
	logger.Debug("✅ maker.LessDNS: 创建目录 %s 成功", location)

	logger.Debug("☑️ maker.LessDNS: 写入 deployment.yaml 文件..., dest = %s", filepath.Join(location, "deployment.yaml"))
	if err = os.WriteFile(filepath.Join(location, "deployment.yaml"), resource.YAMLLessDNS, 0644); err != nil {
		logger.Debug("❌ maker.LessDNS: 写入 deployment.yaml 失败, dest = %s, err = %v", filepath.Join(location, "deployment.yaml"), err)
		return err
	}
	logger.Debug("✅ maker.LessDNS: 写入 deployment.yaml 文件成功, dest = %s", filepath.Join(location, "deployment.yaml"))

	logger.Info("✅ maker.LessDNS: 构建 less-dns 成功!!!")
	return nil
}
