package com.lingyue.extract.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

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

/**
 * 报告生成任务实体
 * 
 * 使用模板生成新报告的一次任务
 * 记录来源文件映射、变量提取结果、生成的文档
 * 
 * @author lingyue
 * @since 2026-01-23
 */
@Data
@TableName(value = "generations", autoResultMap = true)
@Schema(description = "报告生成任务")
public class Generation {
    
    @Schema(description = "ID")
    @TableId
    private String id;
    
    @Schema(description = "模板ID")
    @TableField("template_id")
    private String templateId;
    
    @Schema(description = "用户ID")
    @TableField("user_id")
    private String userId;
    
    @Schema(description = "任务名称")
    @TableField("name")
    private String name;
    
    // ==================== 来源文件映射 ====================
    
    @Schema(description = "来源文件映射：别名 → 文档ID，如 {\"可研批复\": \"doc_123\"}")
    @TableField(value = "source_file_map", typeHandler = JacksonTypeHandler.class)
    private Map<String, String> sourceFileMap;
    
    // ==================== 变量提取结果 ====================
    
    @Schema(description = "变量值，包含值、置信度、状态等")
    @TableField(value = "variable_values", typeHandler = JacksonTypeHandler.class)
    private Map<String, VariableValue> variableValues;
    
    // ==================== 生成的文档 ====================
    
    @Schema(description = "输出文档ID")
    @TableField("output_document_id")
    private String outputDocumentId;
    
    @Schema(description = "输出文件路径")
    @TableField("output_file_path")
    private String outputFilePath;
    
    // ==================== 状态 ====================
    
    @Schema(description = "状态: pending-待执行, extracting-提取中, review-待确认, completed-已完成, error-错误")
    @TableField("status")
    private String status;
    
    @Schema(description = "错误信息")
    @TableField("error_message")
    private String errorMessage;
    
    @Schema(description = "进度百分比 0-100")
    @TableField("progress")
    private Integer progress;
    
    // ==================== 时间 ====================
    
    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private Date createTime;
    
    @Schema(description = "完成时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("completed_at")
    private Date completedAt;
    
    // ==================== 状态常量 ====================
    
    public static final String STATUS_PENDING = "pending";
    public static final String STATUS_EXTRACTING = "extracting";
    public static final String STATUS_REVIEW = "review";
    public static final String STATUS_COMPLETED = "completed";
    public static final String STATUS_ERROR = "error";
    
    /**
     * 变量值内部类
     */
    @Data
    public static class VariableValue {
        
        @Schema(description = "提取的值")
        private String value;
        
        @Schema(description = "AI置信度 0-1")
        private Double confidence;
        
        @Schema(description = "来源内容预览")
        private String sourcePreview;
        
        @Schema(description = "状态: pending-待提取, extracted-已提取, modified-已修改, error-错误")
        private String status;
        
        @Schema(description = "错误信息")
        private String errorMessage;
        
        // 状态常量
        public static final String STATUS_PENDING = "pending";
        public static final String STATUS_EXTRACTED = "extracted";
        public static final String STATUS_MODIFIED = "modified";
        public static final String STATUS_ERROR = "error";
    }
}
