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.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 提取结果实体
 * 
 * 规则执行后的提取值，保存来源追溯信息
 * 
 * @author lingyue
 * @since 2026-01-22
 */
@Data
@TableName(value = "extract_results", autoResultMap = true)
@Schema(description = "提取结果")
public class ExtractResult {
    
    @Schema(description = "ID")
    @TableId
    private String id;
    
    @Schema(description = "规则ID")
    @TableField("rule_id")
    private String ruleId;
    
    @Schema(description = "项目ID")
    @TableField("project_id")
    private String projectId;
    
    // ==================== 提取结果 ====================
    
    @Schema(description = "提取出的值")
    @TableField("extracted_value")
    private String extractedValue;
    
    @Schema(description = "值类型: text-文本, table-表格, image-图片, list-列表")
    @TableField("value_type")
    private String valueType;
    
    // ==================== 来源追溯 ====================
    
    @Schema(description = "来源原文内容")
    @TableField("source_content")
    private String sourceContent;
    
    @Schema(description = "来源位置信息")
    @TableField(value = "source_location", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> sourceLocation;
    
    // ==================== 质量评估 ====================
    
    @Schema(description = "AI提取置信度 0-1")
    @TableField("confidence")
    private BigDecimal confidence;
    
    // ==================== 状态 ====================
    
    @Schema(description = "状态: extracted-已提取, confirmed-已确认, rejected-已拒绝, modified-已修正")
    @TableField("status")
    private String status;
    
    // ==================== 人工处理 ====================
    
    @Schema(description = "人工修正后的值")
    @TableField("modified_value")
    private String modifiedValue;
    
    @Schema(description = "确认时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("confirmed_at")
    private Date confirmedAt;
    
    @Schema(description = "确认人")
    @TableField("confirmed_by")
    private String confirmedBy;
    
    @Schema(description = "拒绝原因")
    @TableField("reject_reason")
    private String rejectReason;
    
    // ==================== 元数据 ====================
    
    @Schema(description = "元数据（AI输出、处理日志等）")
    @TableField(value = "metadata", typeHandler = JacksonTypeHandler.class)
    private Map<String, Object> metadata;
    
    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private Date createTime;
    
    // ==================== 状态常量 ====================
    
    public static final String STATUS_EXTRACTED = "extracted";
    public static final String STATUS_CONFIRMED = "confirmed";
    public static final String STATUS_REJECTED = "rejected";
    public static final String STATUS_MODIFIED = "modified";
    
    /**
     * 获取最终值（优先使用修正后的值）
     */
    public String getFinalValue() {
        if (modifiedValue != null && !modifiedValue.isEmpty()) {
            return modifiedValue;
        }
        return extractedValue;
    }
}
