package tech.easyflow.ai.publish;

import org.springframework.stereotype.Service;
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 BotPublishAppService {

    private final AiResourceLifecycleService aiResourceLifecycleService;

    public BotPublishAppService(AiResourceLifecycleService aiResourceLifecycleService) {
        this.aiResourceLifecycleService = aiResourceLifecycleService;
    }

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

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

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

    private void assertId(BigInteger id) {
        if (id == null) {
            throw new BusinessException("聊天助手审批时资源ID不能为空");
        }
    }
}
