package com.agent.management.external.dto;

import lombok.Data;

import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * 外部 API 全部 DTO 集中定义，便于维护。
 * 所有响应均使用 {@link ApiError} 包络：
 * <pre>
 * { "code": "OK", "message": "", "data": { ... } }
 * </pre>
 */
public final class ExternalDtos {

    private ExternalDtos() {}

    // ========== 创建运行 ==========

    @Data
    public static class CreateRunRequest {
        private Map<String, Object> variables;
        /** true（默认）：仅创建不启动；false：立即启动，直接进入 SSE 流 */
        private Boolean async = true;
        /** 全局失败策略覆盖：abort / skip */
        private String failStrategy;
        /** 工作空间保留小时数，默认 720（30 天） */
        private Integer ttlHours = 720;
    }

    @Data
    public static class CreateRunResponse {
        private String runId;
        private Long workflowId;
        /** 工作流唯一标识（kebab-case），URL 路径使用此字段 */
        private String workflowName;
        /** 工作流中文显示名 */
        private String displayName;
        private String status;
        private Instant createdAt;
        private Links links;
    }

    @Data
    public static class Links {
        private String start;
        private String stream;
        private String result;
        private String workspace;
    }

    // ========== 启动运行 ==========

    @Data
    public static class StartRunResponse {
        private String runId;
        private String status;
        private String streamUrl;
    }

    // ========== 查询运行结果 ==========

    @Data
    public static class RunResultResponse {
        private String runId;
        private Long workflowId;
        /** 工作流唯一标识（kebab-case） */
        private String workflowName;
        /** 工作流中文显示名 */
        private String displayName;
        private String status;
        private Instant startedAt;
        private Instant completedAt;
        private String error;
        /** 工作流最终输出变量 */
        private Map<String, Object> outputs;
        private List<NodeResult> nodes;
        private WorkspaceInfo workspace;
    }

    @Data
    public static class NodeResult {
        private String nodeId;
        private String nodeType;
        private String label;
        private String status;
        private Instant startedAt;
        private Instant completedAt;
        private Map<String, Object> output;
        private String error;
        private int logsCount;
        private String logsUrl;
    }

    @Data
    public static class WorkspaceInfo {
        private int fileCount;
        private long totalBytes;
        private String downloadUrl;
    }

    // ========== 节点日志 ==========

    @Data
    public static class NodeLogsResponse {
        private String runId;
        private String nodeId;
        private List<LogEntry> logs;
    }

    @Data
    public static class LogEntry {
        private String type;
        private Instant timestamp;
        private String message;
        private String detail;
    }

    // ========== 可调用工作流列表 ==========

    @Data
    public static class WorkflowListResponse {
        private int total;
        private List<WorkflowSummary> items;
    }

    @Data
    public static class WorkflowSummary {
        private Long id;
        /** kebab-case 唯一标识，URL 路径使用此字段 */
        private String name;
        /** 中文显示名 */
        private String displayName;
        private String description;
        private List<FieldDef> inputs;
        private List<FieldDef> outputs;
    }

    @Data
    public static class FieldDef {
        private String name;
        private String type;
        private boolean required;
        private String description;
    }
}
