package tech.easyflow.admin.controller.agent;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import tech.easyflow.admin.controller.ai.support.AiResourceCreatorNameSupport;
import tech.easyflow.agent.entity.Agent;
import tech.easyflow.agent.entity.AgentKnowledgeBinding;
import tech.easyflow.agent.entity.AgentToolBinding;
import tech.easyflow.agent.publish.AgentPublishAppService;
import tech.easyflow.agent.runtime.AgentChatRequest;
import tech.easyflow.agent.runtime.AgentDraftChatRequest;
import tech.easyflow.agent.runtime.AgentRunService;
import tech.easyflow.agent.service.AgentApprovalStateService;
import tech.easyflow.agent.service.AgentKnowledgeBindingService;
import tech.easyflow.agent.service.AgentService;
import tech.easyflow.agent.service.AgentToolBindingService;
import tech.easyflow.ai.enums.PublishStatus;
import tech.easyflow.approval.entity.vo.ApprovalActionResult;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.web.controller.BaseCurdController;
import tech.easyflow.common.web.jsonbody.JsonBody;
import tech.easyflow.system.entity.vo.RoleCategoryAccessSnapshot;
import tech.easyflow.system.enums.CategoryResourceType;
import tech.easyflow.system.enums.ResourceAction;
import tech.easyflow.system.service.CategoryPermissionService;
import tech.easyflow.system.service.ResourceAccessService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static tech.easyflow.agent.entity.table.AgentTableDef.AGENT;

/**
 * Agent 管理端控制器。
 */
@RestController
@RequestMapping("/api/v1/agent")
public class AgentController extends BaseCurdController<AgentService, Agent> {

    @Resource
    private AgentToolBindingService agentToolBindingService;
    @Resource
    private AgentKnowledgeBindingService agentKnowledgeBindingService;
    @Resource
    private AgentRunService agentRunService;
    @Resource
    private AgentPublishAppService agentPublishAppService;
    @Resource
    private ResourceAccessService resourceAccessService;
    @Resource
    private CategoryPermissionService categoryPermissionService;
    @Resource
    private AgentApprovalStateService agentApprovalStateService;
    @Resource
    private AiResourceCreatorNameSupport aiResourceCreatorNameSupport;

    /**
     * 创建 Agent 控制器。
     *
     * @param service Agent 服务
     */
    public AgentController(AgentService service) {
        super(service);
    }

    /**
     * 获取 Agent 详情。
     *
     * @param id Agent ID
     * @return Agent 详情
     */
    @GetMapping("/getDetail")
    public Result<Agent> getDetail(BigInteger id) {
        Agent agent = service.getDetail(id);
        agentApprovalStateService.fillAgentApprovalState(agent);
        return Result.ok(agent);
    }

    /**
     * 保存 Agent 草稿。
     *
     * @param agent Agent 草稿
     * @return Agent 详情
     */
    @Override
    @PostMapping("save")
    public Result<?> save(@JsonBody Agent agent) {
        return Result.ok(service.saveDraft(agent));
    }

    /**
     * 更新 Agent 草稿。
     *
     * @param agent Agent 草稿
     * @return Agent 详情
     */
    @Override
    @PostMapping("update")
    public Result<?> update(@JsonBody Agent agent) {
        return Result.ok(service.updateDraft(agent));
    }

    /**
     * 查询 Agent 列表。
     *
     * @param entity 查询条件
     * @param asTree 是否转树
     * @param sortKey 排序字段
     * @param sortType 排序方式
     * @return Agent 列表
     */
    @Override
    public Result<List<Agent>> list(Agent entity, Boolean asTree, String sortKey, String sortType) {
        HttpServletRequest request = currentRequest();
        QueryWrapper queryWrapper = request == null ? QueryWrapper.create() : buildQueryWrapper(request);
        if (!applyCategoryPermission(queryWrapper)) {
            return Result.ok(Collections.emptyList());
        }
        applyPublishedOnlyFilter(queryWrapper);
        queryWrapper.orderBy(buildOrderBy(sortKey, sortType, getDefaultOrderBy()));
        List<Agent> agents = service.list(queryWrapper);
        if (isPublishedOnlyRequest()) {
            agents = agents.stream().map(agent -> service.fromSnapshot(agent.getPublishedSnapshotJson())).toList();
        }
        agentApprovalStateService.fillAgentApprovalState(agents);
        aiResourceCreatorNameSupport.fillAgentCreatorNames(agents);
        return Result.ok(agents);
    }

    /**
     * 运行 Agent 纯文本聊天。
     *
     * @param request 聊天请求
     * @return SSE Emitter
     */
    @PostMapping("chat")
    public SseEmitter chat(@JsonBody AgentChatRequest request) {
        return agentRunService.chat(request);
    }

    /**
     * 运行 Agent 草稿态纯文本试用。
     *
     * @param request 草稿试用请求
     * @return SSE Emitter
     */
    @PostMapping("/chat/draft")
    public SseEmitter chatDraft(@JsonBody AgentDraftChatRequest request) {
        return agentRunService.chatDraft(request);
    }

    /**
     * 清理 Agent 草稿试运行会话。
     *
     * @param sessionId 草稿试运行会话 ID
     * @return 操作结果
     */
    @PostMapping("/chat/draft/clear")
    public Result<Void> clearDraftSession(@JsonBody(value = "sessionId", required = true) String sessionId) {
        agentRunService.clearDraftSession(sessionId);
        return Result.ok();
    }

    /**
     * 批准工具执行。
     *
     * @param requestId 请求 ID
     * @param resumeToken 恢复令牌
     * @return 操作结果
     */
    @PostMapping("/run/approve")
    public Result<Void> approve(@JsonBody("requestId") String requestId,
                                @JsonBody(value = "resumeToken", required = true) String resumeToken) {
        agentRunService.approve(requestId, resumeToken);
        return Result.ok();
    }

    /**
     * 拒绝工具执行。
     *
     * @param requestId 请求 ID
     * @param resumeToken 恢复令牌
     * @param reason 拒绝原因
     * @return 操作结果
     */
    @PostMapping("/run/reject")
    public Result<Void> reject(@JsonBody("requestId") String requestId,
                               @JsonBody(value = "resumeToken", required = true) String resumeToken,
                               @JsonBody("reason") String reason) {
        agentRunService.reject(requestId, resumeToken, reason);
        return Result.ok();
    }

    /**
     * 更新 Agent 工具绑定。
     *
     * @param agentId Agent ID
     * @param bindings 工具绑定
     * @return 保存后的启用绑定
     */
    @PostMapping("/toolBinding/update")
    @SaCheckPermission("/api/v1/agent/save")
    public Result<List<AgentToolBinding>> updateToolBinding(@JsonBody(value = "agentId", required = true) BigInteger agentId,
                                                            @JsonBody("bindings") List<AgentToolBinding> bindings) {
        return Result.ok(agentToolBindingService.replaceBindings(agentId, bindings));
    }

    /**
     * 更新 Agent 知识库绑定。
     *
     * @param agentId Agent ID
     * @param bindings 知识库绑定
     * @return 保存后的启用绑定
     */
    @PostMapping("/knowledgeBinding/update")
    @SaCheckPermission("/api/v1/agent/save")
    public Result<List<AgentKnowledgeBinding>> updateKnowledgeBinding(@JsonBody(value = "agentId", required = true) BigInteger agentId,
                                                                      @JsonBody("bindings") List<AgentKnowledgeBinding> bindings) {
        return Result.ok(agentKnowledgeBindingService.replaceBindings(agentId, bindings));
    }

    /**
     * 提交发布审批。
     *
     * @param id Agent ID
     * @return 审批实例 ID
     */
    @PostMapping("/submitPublishApproval")
    @SaCheckPermission("/api/v1/agent/save")
    public Result<BigInteger> submitPublishApproval(@JsonBody("id") BigInteger id) {
        return buildApprovalActionResult(agentPublishAppService.submitPublishApproval(id), "已提交发布审批", "已直接发布");
    }

    /**
     * 提交下线审批。
     *
     * @param id Agent ID
     * @return 审批实例 ID
     */
    @PostMapping("/submitOfflineApproval")
    @SaCheckPermission("/api/v1/agent/save")
    public Result<BigInteger> submitOfflineApproval(@JsonBody("id") BigInteger id) {
        return buildApprovalActionResult(agentPublishAppService.submitOfflineApproval(id), "已提交下线审批", "已直接下线");
    }

    /**
     * 提交删除审批。
     *
     * @param id Agent ID
     * @return 审批实例 ID
     */
    @PostMapping("/submitDeleteApproval")
    @SaCheckPermission("/api/v1/agent/remove")
    public Result<BigInteger> submitDeleteApproval(@JsonBody("id") BigInteger id) {
        return buildApprovalActionResult(agentPublishAppService.submitDeleteApproval(id), "已提交删除审批", "已直接删除");
    }

    @Override
    protected Result<?> onRemoveBefore(Collection<Serializable> ids) {
        for (Serializable id : ids) {
            Agent agent = service.getById(String.valueOf(id));
            if (agent != null) {
                resourceAccessService.assertAccess(CategoryResourceType.AGENT, agent, ResourceAction.MANAGE, "无权限删除该 Agent");
            }
        }
        agentToolBindingService.remove(QueryWrapper.create().in("agent_id", ids));
        agentKnowledgeBindingService.remove(QueryWrapper.create().in("agent_id", ids));
        return super.onRemoveBefore(ids);
    }

    /**
     * 查询 Agent 分页。
     *
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return Agent 分页
     */
    @Override
    protected Page<Agent> queryPage(Page<Agent> page, QueryWrapper queryWrapper) {
        if (!applyCategoryPermission(queryWrapper)) {
            return new Page<>(Collections.emptyList(), page.getPageNumber(), page.getPageSize(), 0L);
        }
        applyPublishedOnlyFilter(queryWrapper);
        Page<Agent> result = super.queryPage(page, queryWrapper);
        if (isPublishedOnlyRequest()) {
            result.setRecords(result.getRecords().stream().map(agent -> service.fromSnapshot(agent.getPublishedSnapshotJson())).toList());
        }
        agentApprovalStateService.fillAgentApprovalState(result.getRecords());
        aiResourceCreatorNameSupport.fillAgentCreatorNames(result.getRecords());
        return result;
    }

    private boolean applyCategoryPermission(QueryWrapper queryWrapper) {
        RoleCategoryAccessSnapshot access = categoryPermissionService.getCurrentAccess(CategoryResourceType.AGENT.getCode());
        if (!access.isRestricted()) {
            return true;
        }
        if (access.getCategoryIds().isEmpty()) {
            queryWrapper.eq(Agent::getCreatedBy, access.getAccountId());
            return true;
        }
        queryWrapper.and(AGENT.CREATED_BY.eq(access.getAccountId()).or(AGENT.CATEGORY_ID.in(access.getCategoryIds())));
        return true;
    }

    private void applyPublishedOnlyFilter(QueryWrapper queryWrapper) {
        if (isPublishedOnlyRequest()) {
            queryWrapper.eq("publish_status", PublishStatus.PUBLISHED.getCode());
        }
    }

    private boolean isPublishedOnlyRequest() {
        HttpServletRequest request = currentRequest();
        if (request == null) {
            return false;
        }
        return "true".equalsIgnoreCase(request.getParameter("publishedOnly"));
    }

    /**
     * 获取当前 HTTP 请求。
     *
     * @return 当前请求，不在 Web 请求上下文中时返回 null
     */
    private HttpServletRequest currentRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        return attributes.getRequest();
    }

    private Result<BigInteger> buildApprovalActionResult(ApprovalActionResult actionResult,
                                                         String approvalMessage,
                                                         String directMessage) {
        return Result.ok(actionResult.isApprovalRequired() ? approvalMessage : directMessage, actionResult.getInstanceId());
    }
}
