package tech.easyflow.admin.controller.agent;

import tech.easyflow.agent.entity.Agent;
import tech.easyflow.agent.entity.AgentKnowledgeBinding;
import tech.easyflow.agent.entity.AgentSkillBinding;
import tech.easyflow.agent.entity.AgentToolBinding;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 管理端 Agent 草稿安全详情。
 *
 * <p>该视图明确排除发布快照以及各绑定的内部资源快照。</p>
 */
public record AgentDetailView(
        BigInteger id,
        BigInteger deptId,
        String name,
        String description,
        String avatar,
        BigInteger categoryId,
        BigInteger modelId,
        Map<String, Object> modelConfigJson,
        Map<String, Object> generationConfigJson,
        Map<String, Object> promptConfigJson,
        Map<String, Object> memoryConfigJson,
        Map<String, Object> executionConfigJson,
        Map<String, Object> interactionConfigJson,
        Integer status,
        String visibilityScope,
        String publishStatus,
        BigInteger currentApprovalInstanceId,
        Date publishedAt,
        BigInteger publishedBy,
        Date created,
        BigInteger createdBy,
        Date modified,
        BigInteger modifiedBy,
        Boolean approvalPending,
        String currentApprovalActionType,
        String displayPublishStatus,
        String createdByName,
        List<ToolBindingView> toolBindings,
        List<KnowledgeBindingView> knowledgeBindings,
        List<SkillBindingView> skillBindings) {

    /**
     * 从领域实体构造安全详情。
     *
     * @param agent Agent 领域实体
     * @return 安全详情
     */
    public static AgentDetailView from(Agent agent) {
        return new AgentDetailView(agent.getId(), agent.getDeptId(), agent.getName(), agent.getDescription(),
                agent.getAvatar(), agent.getCategoryId(), agent.getModelId(), agent.getModelConfigJson(),
                agent.getGenerationConfigJson(), agent.getPromptConfigJson(), agent.getMemoryConfigJson(),
                agent.getExecutionConfigJson(), agent.getInteractionConfigJson(), agent.getStatus(),
                agent.getVisibilityScope(), agent.getPublishStatus(), agent.getCurrentApprovalInstanceId(),
                agent.getPublishedAt(), agent.getPublishedBy(), agent.getCreated(), agent.getCreatedBy(),
                agent.getModified(), agent.getModifiedBy(), agent.getApprovalPending(),
                agent.getCurrentApprovalActionType(), agent.getDisplayPublishStatus(), agent.getCreatedByName(),
                mapTools(agent.getToolBindings()), mapKnowledges(agent.getKnowledgeBindings()),
                mapSkills(agent.getSkillBindings()));
    }

    private static List<ToolBindingView> mapTools(List<AgentToolBinding> bindings) {
        return bindings == null ? List.of() : bindings.stream().map(ToolBindingView::from).toList();
    }

    private static List<KnowledgeBindingView> mapKnowledges(List<AgentKnowledgeBinding> bindings) {
        return bindings == null ? List.of() : bindings.stream().map(KnowledgeBindingView::from).toList();
    }

    private static List<SkillBindingView> mapSkills(List<AgentSkillBinding> bindings) {
        return bindings == null ? List.of() : bindings.stream().map(SkillBindingView::from).toList();
    }

    /** Agent 直接 Tool 草稿绑定。 */
    public record ToolBindingView(BigInteger id, String toolType, BigInteger targetId, String toolName,
                                  Boolean enabled, Boolean hitlEnabled, Map<String, Object> hitlConfigJson,
                                  Map<String, Object> optionsJson, Integer sortNo,
                                  Map<String, Object> resourceSummary) {
        /** @param value 实体 @return 安全绑定 */
        static ToolBindingView from(AgentToolBinding value) {
            return new ToolBindingView(value.getId(), value.getToolType(), value.getTargetId(), value.getToolName(),
                    value.getEnabled(), value.getHitlEnabled(), value.getHitlConfigJson(), value.getOptionsJson(),
                    value.getSortNo(), value.getResourceSummary());
        }
    }

    /** Agent 知识库草稿绑定。 */
    public record KnowledgeBindingView(BigInteger id, BigInteger knowledgeId, String retrievalMode,
                                       Boolean enabled, Map<String, Object> optionsJson, Integer sortNo,
                                       Map<String, Object> resourceSummary) {
        /** @param value 实体 @return 安全绑定 */
        static KnowledgeBindingView from(AgentKnowledgeBinding value) {
            return new KnowledgeBindingView(value.getId(), value.getKnowledgeId(), value.getRetrievalMode(),
                    value.getEnabled(), value.getOptionsJson(), value.getSortNo(), value.getResourceSummary());
        }
    }

    /** Agent Skill 草稿绑定。 */
    public record SkillBindingView(BigInteger id, BigInteger skillId, Integer sortNo,
                                   Map<String, Object> resourceSummary) {
        /** @param value 实体 @return 安全绑定 */
        static SkillBindingView from(AgentSkillBinding value) {
            return new SkillBindingView(value.getId(), value.getSkillId(), value.getSortNo(),
                    value.getResourceSummary());
        }
    }
}
