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 com.lingyue.extract.dto.config.VariableLocation;
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 = "variables", autoResultMap = true)
@Schema(description = "模板变量")
public class Variable {
    
    @Schema(description = "ID")
    @TableId
    private String id;
    
    @Schema(description = "模板ID")
    @TableField("template_id")
    private String templateId;
    
    // ==================== 变量标识 ====================
    
    @Schema(description = "变量名（程序用），模板内唯一")
    @TableField("name")
    private String name;
    
    @Schema(description = "显示名称")
    @TableField("display_name")
    private String displayName;
    
    @Schema(description = "变量分组")
    @TableField("variable_group")
    private String variableGroup;
    
    @Schema(description = "变量类别（前端显示用）: entity-核心实体, concept-概念/技术, data-数据/指标, location-地点/组织, asset-资源模板")
    @TableField("category")
    private String category;
    
    // ==================== 在示例报告中的位置 ====================
    
    @Schema(description = "变量在文档中的位置")
    @TableField(value = "location", typeHandler = JacksonTypeHandler.class)
    private VariableLocation location;
    
    // ==================== 示例值 ====================
    
    @Schema(description = "示例值（原文档中的值）")
    @TableField("example_value")
    private String exampleValue;
    
    @Schema(description = "值类型: text-文本, date-日期, number-数字, table-表格")
    @TableField("value_type")
    private String valueType;
    
    // ==================== 数据来源 ====================
    
    @Schema(description = "来源文件别名，对应 SourceFile.alias")
    @TableField("source_file_alias")
    private String sourceFileAlias;
    
    @Schema(description = "来源类型: document-从来源文件提取, manual-手动输入, reference-引用其他变量, fixed-固定值")
    @TableField("source_type")
    private String sourceType;
    
    @Schema(description = "来源配置")
    @TableField(value = "source_config", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> sourceConfig;
    
    // ==================== 提取方式 ====================
    
    @Schema(description = "提取类型: direct-直接提取, ai_extract-AI字段提取, ai_summarize-AI总结")
    @TableField("extract_type")
    private String extractType;
    
    @Schema(description = "提取配置")
    @TableField(value = "extract_config", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> extractConfig;
    
    // ==================== 其他 ====================
    
    @Schema(description = "显示顺序")
    @TableField("display_order")
    private Integer displayOrder;
    
    @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("update_time")
    private Date updateTime;
    
    // ==================== 来源类型常量 ====================
    
    public static final String SOURCE_TYPE_DOCUMENT = "document";
    public static final String SOURCE_TYPE_MANUAL = "manual";
    public static final String SOURCE_TYPE_REFERENCE = "reference";
    public static final String SOURCE_TYPE_FIXED = "fixed";
    
    // ==================== 提取类型常量 ====================
    
    public static final String EXTRACT_TYPE_DIRECT = "direct";
    public static final String EXTRACT_TYPE_AI_EXTRACT = "ai_extract";
    public static final String EXTRACT_TYPE_AI_SUMMARIZE = "ai_summarize";
    
    // ==================== 值类型常量 ====================
    
    public static final String VALUE_TYPE_TEXT = "text";
    public static final String VALUE_TYPE_DATE = "date";
    public static final String VALUE_TYPE_NUMBER = "number";
    public static final String VALUE_TYPE_TABLE = "table";
    
    // ==================== 变量类别常量（前端显示用） ====================
    
    /** 核心实体（蓝色） - 智慧园区、公司名等 */
    public static final String CATEGORY_ENTITY = "entity";
    /** 概念/技术（紫色） - 产业升级、智能化管理等 */
    public static final String CATEGORY_CONCEPT = "concept";
    /** 数据/指标（绿色） - 金额、面积、增长率等 */
    public static final String CATEGORY_DATA = "data";
    /** 地点/组织（橙色） - 华南地区、华南事业部等 */
    public static final String CATEGORY_LOCATION = "location";
    /** 资源模板（粉色） - 图表、结论模板等 */
    public static final String CATEGORY_ASSET = "asset";
}
