package com.lingyue.extract.dto.response;

import com.lingyue.extract.entity.Generation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Date;
import java.util.Map;

/**
 * 生成任务响应
 * 
 * @author lingyue
 * @since 2026-01-24
 */
@Data
@Schema(description = "生成任务响应")
public class GenerationResponse {
    
    @Schema(description = "任务ID")
    private String id;
    
    @Schema(description = "模板ID")
    private String templateId;
    
    @Schema(description = "模板名称")
    private String templateName;
    
    @Schema(description = "用户ID")
    private String userId;
    
    @Schema(description = "任务名称")
    private String name;
    
    @Schema(description = "来源文件映射")
    private Map<String, String> sourceFileMap;
    
    @Schema(description = "变量值")
    private Map<String, Generation.VariableValue> variableValues;
    
    @Schema(description = "输出文档ID")
    private String outputDocumentId;
    
    @Schema(description = "输出文件路径")
    private String outputFilePath;
    
    @Schema(description = "状态: pending/extracting/review/generating/completed/error")
    private String status;
    
    @Schema(description = "错误信息")
    private String errorMessage;
    
    @Schema(description = "进度 0-100")
    private Integer progress;
    
    @Schema(description = "创建时间")
    private Date createTime;
    
    @Schema(description = "完成时间")
    private Date completedAt;
    
    // ==================== 统计信息 ====================
    
    @Schema(description = "变量总数")
    private Integer totalVariables;
    
    @Schema(description = "已提取变量数")
    private Integer extractedVariables;
    
    /**
     * 从实体转换
     */
    public static GenerationResponse fromEntity(Generation generation) {
        GenerationResponse response = new GenerationResponse();
        response.setId(generation.getId());
        response.setTemplateId(generation.getTemplateId());
        response.setUserId(generation.getUserId());
        response.setName(generation.getName());
        response.setSourceFileMap(generation.getSourceFileMap());
        response.setVariableValues(generation.getVariableValues());
        response.setOutputDocumentId(generation.getOutputDocumentId());
        response.setOutputFilePath(generation.getOutputFilePath());
        response.setStatus(generation.getStatus());
        response.setErrorMessage(generation.getErrorMessage());
        response.setProgress(generation.getProgress());
        response.setCreateTime(generation.getCreateTime());
        response.setCompletedAt(generation.getCompletedAt());
        
        // 统计变量
        if (generation.getVariableValues() != null) {
            response.setTotalVariables(generation.getVariableValues().size());
            long extracted = generation.getVariableValues().values().stream()
                    .filter(v -> v.getValue() != null && !v.getValue().isEmpty())
                    .count();
            response.setExtractedVariables((int) extracted);
        }
        
        return response;
    }
}
