package tech.easyflow.skill.publish;

import org.springframework.stereotype.Service;
import tech.easyflow.ai.publish.AiResourceLifecycleService;
import tech.easyflow.approval.entity.vo.ApprovalActionResult;
import tech.easyflow.approval.enums.ApprovalActionType;
import tech.easyflow.approval.enums.ApprovalResourceType;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.math.BigInteger;

/**
 * Skill 发布生命周期应用服务。
 */
@Service
public class SkillPublishAppService {

    private final AiResourceLifecycleService aiResourceLifecycleService;

    /**
     * 创建 Skill 发布应用服务。
     *
     * @param aiResourceLifecycleService AI 资源生命周期服务
     */
    public SkillPublishAppService(AiResourceLifecycleService aiResourceLifecycleService) {
        this.aiResourceLifecycleService = aiResourceLifecycleService;
    }

    /**
     * 提交 Skill 发布审批。
     *
     * @param id Skill ID
     * @return 审批动作结果
     */
    public ApprovalActionResult submitPublishApproval(BigInteger id) {
        return submit(id, ApprovalActionType.PUBLISH);
    }

    /**
     * 提交 Skill 下线审批。
     *
     * @param id Skill ID
     * @return 审批动作结果
     */
    public ApprovalActionResult submitOfflineApproval(BigInteger id) {
        return submit(id, ApprovalActionType.OFFLINE);
    }

    /**
     * 提交 Skill 删除审批。
     *
     * @param id Skill ID
     * @return 审批动作结果
     */
    public ApprovalActionResult submitDeleteApproval(BigInteger id) {
        return submit(id, ApprovalActionType.DELETE);
    }

    private ApprovalActionResult submit(BigInteger id, ApprovalActionType actionType) {
        if (id == null) {
            throw new BusinessException("Skill 审批时资源ID不能为空");
        }
        return aiResourceLifecycleService.submitAction(
                ApprovalResourceType.SKILL.getCode(),
                id,
                actionType.getCode(),
                SaTokenUtil.getLoginAccount().getId()
        );
    }
}

