package tech.easyflow.ai.publish;

import org.springframework.stereotype.Service;
import tech.easyflow.ai.entity.DocumentCollection;
import tech.easyflow.ai.enums.PublishStatus;
import tech.easyflow.ai.service.DocumentCollectionService;
import tech.easyflow.ai.service.ResourceOfflineImpactService;
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 KnowledgePublishAppService {

    private final DocumentCollectionService documentCollectionService;
    private final ResourceOfflineImpactService resourceOfflineImpactService;
    private final AiResourceLifecycleService aiResourceLifecycleService;

    public KnowledgePublishAppService(DocumentCollectionService documentCollectionService,
                                      ResourceOfflineImpactService resourceOfflineImpactService,
                                      AiResourceLifecycleService aiResourceLifecycleService) {
        this.documentCollectionService = documentCollectionService;
        this.resourceOfflineImpactService = resourceOfflineImpactService;
        this.aiResourceLifecycleService = aiResourceLifecycleService;
    }

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

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

    /**
     * 检查知识库下线影响。
     *
     * @param id 知识库 ID
     * @return 下线影响结果
     */
    public OfflineImpactCheckVo checkOfflineImpact(BigInteger id) {
        assertId(id);
        DocumentCollection knowledge = documentCollectionService.getById(id);
        if (knowledge == null) {
            throw new BusinessException("知识库不存在");
        }
        if (PublishStatus.from(knowledge.getPublishStatus()) != PublishStatus.PUBLISHED) {
            throw new BusinessException("当前知识库尚未发布，无法下线");
        }
        return resourceOfflineImpactService.checkKnowledgeImpact(id);
    }

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

    private void assertId(BigInteger id) {
        if (id == null) {
            throw new BusinessException("知识库审批时资源ID不能为空");
        }
    }
}
