package com.lingyue.extract.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.lingyue.common.domain.entity.SimpleModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Map;

/**
 * 提取规则实体
 * 
 * 描述如何从来源文档中提取数据的配置
 * 
 * @author lingyue
 * @since 2026-01-22
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName(value = "extract_rules", autoResultMap = true)
@Schema(description = "数据提取规则")
public class ExtractRule extends SimpleModel {
    
    @Schema(description = "项目ID")
    @TableField("project_id")
    private String projectId;
    
    @Schema(description = "来源文档ID（可为空，表示引用/固定/手动类型）")
    @TableField("source_doc_id")
    private String sourceDocId;
    
    // ==================== 目标字段 ====================
    
    @Schema(description = "目标字段Key（程序用）")
    @TableField("target_field_key")
    private String targetFieldKey;
    
    @Schema(description = "目标字段名称（显示用）")
    @TableField("target_field_name")
    private String targetFieldName;
    
    @Schema(description = "字段分组")
    @TableField("target_field_group")
    private String targetFieldGroup;
    
    @Schema(description = "规则顺序")
    @TableField("rule_index")
    private Integer ruleIndex;
    
    // ==================== 来源配置 ====================
    
    @Schema(description = "来源类型: document-文档, self_reference-引用已提取字段, fixed-固定值, manual-手动输入")
    @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总结, ocr-OCR识别")
    @TableField("extract_type")
    private String extractType;
    
    @Schema(description = "提取配置")
    @TableField(value = "extract_config", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> extractConfig;
    
    // ==================== 结果 ====================
    
    @Schema(description = "状态: pending-待提取, extracting-提取中, extracted-已提取, confirmed-已确认, error-错误")
    @TableField("status")
    private String status;
    
    @Schema(description = "提取出的值")
    @TableField("extracted_value")
    private String extractedValue;
    
    @Schema(description = "值类型: text-文本, table-表格, image-图片, list-列表")
    @TableField("value_type")
    private String valueType;
    
    @Schema(description = "错误信息")
    @TableField("error_message")
    private String errorMessage;
    
    // ==================== 元数据 ====================
    
    @Schema(description = "元数据")
    @TableField(value = "metadata", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> metadata;
    
    // ==================== 来源类型常量 ====================
    
    public static final String SOURCE_TYPE_DOCUMENT = "document";
    public static final String SOURCE_TYPE_SELF_REFERENCE = "self_reference";
    public static final String SOURCE_TYPE_FIXED = "fixed";
    public static final String SOURCE_TYPE_MANUAL = "manual";
    
    // ==================== 提取类型常量 ====================
    
    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 EXTRACT_TYPE_OCR = "ocr";
    
    // ==================== 状态常量 ====================
    
    public static final String STATUS_PENDING = "pending";
    public static final String STATUS_EXTRACTING = "extracting";
    public static final String STATUS_EXTRACTED = "extracted";
    public static final String STATUS_CONFIRMED = "confirmed";
    public static final String STATUS_ERROR = "error";
    
    // ==================== 值类型常量 ====================
    
    public static final String VALUE_TYPE_TEXT = "text";
    public static final String VALUE_TYPE_TABLE = "table";
    public static final String VALUE_TYPE_IMAGE = "image";
    public static final String VALUE_TYPE_LIST = "list";
}
