package maker

import (
	"context"
	"os/exec"

	"gitea.loveuer.com/yizhisec/pkg3/logger"
	"github.com/samber/lo"
)

type imageOpt struct {
	Fallbacks     []string
	IgnoreFailure bool
	ForcePull     bool
	Save          string
}

type ImageOpt func(*imageOpt)

func WithImageFallback(fallbacks ...string) ImageOpt {
	return func(o *imageOpt) {
		o.Fallbacks = lo.Filter(fallbacks, func(item string, _ int) bool { return item != "" })
	}
}

func WithImageSave(filename string) ImageOpt {
	return func(o *imageOpt) {
		o.Save = filename
	}
}

func WithImageForcePull() ImageOpt {
	return func(o *imageOpt) {
		o.ForcePull = true
	}
}

func (m *maker) Image(ctx context.Context, name string, opts ...ImageOpt) error {
	logger.Info("开始获取镜像: %s", name)
	var (
		err  error
		o    = &imageOpt{}
		_cmd *exec.Cmd
	)

	for _, fn := range opts {
		fn(o)
	}

	logger.Debug("maker.Image: name = %s, opt = %#v", name, o)

	if !o.ForcePull {
		_cmd = exec.CommandContext(ctx, "docker", "image", "inspect", name)
		if err = _cmd.Run(); err == nil {
			logger.Info("💾 镜像 %s 已存在", name)
			goto SAVE
		}
	}

	_cmd = exec.CommandContext(ctx, "docker", "pull", name)
	if err = _cmd.Run(); err != nil {
		logger.Debug("获取原始镜像 %s 失败： %v", name, err)
	} else {
		logger.Info("✅ 成功获取镜像: %s", name)
		goto SAVE
	}

	for _, fallback := range o.Fallbacks {
		logger.Info("开始获取镜像: %s (%s)", name, fallback)
		_cmd := exec.CommandContext(ctx, "docker", "pull", fallback)
		if err = _cmd.Run(); err != nil {
			logger.Debug("获取镜像 %s (%s) 失败: %v", name, fallback, err)
			continue
		}

		// pull success, retag image
		_cmd = exec.CommandContext(ctx, "docker", "tag", fallback, name)
		if err = _cmd.Run(); err != nil {
			logger.Debug("重命名镜像 %s => %s 失败: %s", fallback, name, err)
			continue
		}

		logger.Info("✅ 成功获取镜像: %s (%s)", name, fallback)
		break
	}

SAVE:
	if o.Save != "" {
		logger.Debug("保存镜像 %s 到 %s", name, o.Save)
		if err = exec.CommandContext(ctx, "docker", "save", "-o", o.Save, name).Run(); err != nil {
			logger.Debug("保存镜像 %s 到 %s 失败: %v", name, o.Save, err)
		} else {
			logger.Info("✅ 镜像 %s 保存到 %s 成功", name, o.Save)
		}
	}

	if o.IgnoreFailure {
		return nil
	}

	return err
}
