package com.lingyue.extract.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lingyue.extract.entity.ExtractRule;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 提取规则 Repository
 * 
 * @author lingyue
 * @since 2026-01-22
 */
@Mapper
public interface ExtractRuleRepository extends BaseMapper<ExtractRule> {
    
    /**
     * 根据项目ID查询规则列表（按顺序）
     */
    @Select("SELECT * FROM extract_rules WHERE project_id = #{projectId} ORDER BY rule_index")
    List<ExtractRule> findByProjectId(@Param("projectId") String projectId);
    
    /**
     * 根据状态查询规则
     */
    @Select("SELECT * FROM extract_rules WHERE project_id = #{projectId} AND status = #{status} ORDER BY rule_index")
    List<ExtractRule> findByProjectIdAndStatus(@Param("projectId") String projectId, @Param("status") String status);
    
    /**
     * 根据目标字段Key查询
     */
    @Select("SELECT * FROM extract_rules WHERE project_id = #{projectId} AND target_field_key = #{targetFieldKey}")
    ExtractRule findByProjectIdAndTargetFieldKey(@Param("projectId") String projectId, 
                                                  @Param("targetFieldKey") String targetFieldKey);
    
    /**
     * 根据来源文档ID查询
     */
    @Select("SELECT * FROM extract_rules WHERE source_doc_id = #{sourceDocId} ORDER BY rule_index")
    List<ExtractRule> findBySourceDocId(@Param("sourceDocId") String sourceDocId);
    
    /**
     * 根据来源类型查询
     */
    @Select("SELECT * FROM extract_rules WHERE project_id = #{projectId} AND source_type = #{sourceType} ORDER BY rule_index")
    List<ExtractRule> findByProjectIdAndSourceType(@Param("projectId") String projectId, 
                                                    @Param("sourceType") String sourceType);
    
    /**
     * 删除项目的所有规则
     */
    @Delete("DELETE FROM extract_rules WHERE project_id = #{projectId}")
    int deleteByProjectId(@Param("projectId") String projectId);
    
    /**
     * 统计项目规则数量
     */
    @Select("SELECT COUNT(*) FROM extract_rules WHERE project_id = #{projectId}")
    int countByProjectId(@Param("projectId") String projectId);
    
    /**
     * 按状态统计项目规则数量
     */
    @Select("SELECT status, COUNT(*) as count FROM extract_rules WHERE project_id = #{projectId} GROUP BY status")
    List<java.util.Map<String, Object>> countByProjectIdGroupByStatus(@Param("projectId") String projectId);
    
    /**
     * 获取项目最大规则顺序
     */
    @Select("SELECT COALESCE(MAX(rule_index), 0) FROM extract_rules WHERE project_id = #{projectId}")
    int getMaxRuleIndex(@Param("projectId") String projectId);
    
    /**
     * 查询引用了指定字段的规则（用于依赖分析）
     */
    @Select("""
            SELECT * FROM extract_rules 
            WHERE project_id = #{projectId} 
            AND source_type = 'self_reference'
            AND source_config::text LIKE '%' || #{targetFieldKey} || '%'
            """)
    List<ExtractRule> findRulesReferencingField(@Param("projectId") String projectId, 
                                                 @Param("targetFieldKey") String targetFieldKey);
}
