package tech.easyflow.ai.publish;

import org.springframework.stereotype.Service;
import tech.easyflow.ai.entity.Workflow;
import tech.easyflow.ai.enums.PublishStatus;
import tech.easyflow.ai.service.ResourceOfflineImpactService;
import tech.easyflow.ai.service.WorkflowService;
import tech.easyflow.ai.vo.OfflineImpactCheckVo;
import tech.easyflow.approval.entity.vo.ApprovalActionResult;
import tech.easyflow.approval.enums.ApprovalActionType;
import tech.easyflow.approval.enums.ApprovalResourceType;
import tech.easyflow.common.web.exceptions.BusinessException;
import tech.easyflow.common.satoken.util.SaTokenUtil;

import java.math.BigInteger;

/**
 * 工作流发布生命周期应用服务。
 */
@Service
public class WorkflowPublishAppService {

    private final WorkflowService workflowService;
    private final ResourceOfflineImpactService resourceOfflineImpactService;
    private final AiResourceLifecycleService aiResourceLifecycleService;

    public WorkflowPublishAppService(WorkflowService workflowService,
                                     ResourceOfflineImpactService resourceOfflineImpactService,
                                     AiResourceLifecycleService aiResourceLifecycleService) {
        this.workflowService = workflowService;
        this.resourceOfflineImpactService = resourceOfflineImpactService;
        this.aiResourceLifecycleService = aiResourceLifecycleService;
    }

    /**
     * 提交工作流发布审批。
     *
     * @param id 工作流 ID
     * @return 动作执行结果
     */
    public ApprovalActionResult submitPublishApproval(BigInteger id) {
        assertId(id, ApprovalResourceType.WORKFLOW.getCode(), ApprovalActionType.PUBLISH.getCode());
        return aiResourceLifecycleService.submitAction(
                ApprovalResourceType.WORKFLOW.getCode(),
                id,
                ApprovalActionType.PUBLISH.getCode(),
                SaTokenUtil.getLoginAccount().getId()
        );
    }

    /**
     * 提交工作流下线审批。
     *
     * @param id 工作流 ID
     * @return 动作执行结果
     */
    public ApprovalActionResult submitOfflineApproval(BigInteger id) {
        assertId(id, ApprovalResourceType.WORKFLOW.getCode(), ApprovalActionType.OFFLINE.getCode());
        return aiResourceLifecycleService.submitAction(
                ApprovalResourceType.WORKFLOW.getCode(),
                id,
                ApprovalActionType.OFFLINE.getCode(),
                SaTokenUtil.getLoginAccount().getId()
        );
    }

    /**
     * 检查工作流下线影响。
     *
     * @param id 工作流 ID
     * @return 下线影响结果
     */
    public OfflineImpactCheckVo checkOfflineImpact(BigInteger id) {
        assertId(id, ApprovalResourceType.WORKFLOW.getCode(), ApprovalActionType.OFFLINE.getCode());
        Workflow workflow = workflowService.getById(id);
        if (workflow == null) {
            throw new BusinessException("工作流不存在");
        }
        if (PublishStatus.from(workflow.getPublishStatus()) != PublishStatus.PUBLISHED) {
            throw new BusinessException("当前工作流尚未发布，无法下线");
        }
        return resourceOfflineImpactService.checkWorkflowImpact(id);
    }

    /**
     * 提交工作流删除审批。
     *
     * @param id 工作流 ID
     * @return 动作执行结果
     */
    public ApprovalActionResult submitDeleteApproval(BigInteger id) {
        assertId(id, ApprovalResourceType.WORKFLOW.getCode(), ApprovalActionType.DELETE.getCode());
        return aiResourceLifecycleService.submitAction(
                ApprovalResourceType.WORKFLOW.getCode(),
                id,
                ApprovalActionType.DELETE.getCode(),
                SaTokenUtil.getLoginAccount().getId()
        );
    }

    private void assertId(BigInteger id, String resourceType, String actionType) {
        if (id == null) {
            throw new BusinessException(resourceType + " " + actionType + " 审批时资源ID不能为空");
        }
    }
}
