package tech.easyflow.agent.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;

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

    private final AiResourceLifecycleService aiResourceLifecycleService;

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

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

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

    /**
     * 提交 Agent 删除审批。
     *
     * @param id Agent 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("Agent 审批时资源ID不能为空");
        }
        return aiResourceLifecycleService.submitAction(
                ApprovalResourceType.AGENT.getCode(),
                id,
                actionType.getCode(),
                SaTokenUtil.getLoginAccount().getId()
        );
    }
}
