-- ============================================================================
-- LeAudit Platform — 补充文档类型、入口模块表
-- + 全部表中文注释补充
-- 数据库: leaudit_platform @ nas.7bm.co:54302
-- ============================================================================

BEGIN;

-- ============================================================================
-- 1. 新表：入口模块 (leaudit_entry_modules)
-- ============================================================================
CREATE TABLE IF NOT EXISTS leaudit_entry_modules (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    path VARCHAR(255),
    icon_path VARCHAR(255),
    areas JSONB,
    sort_order INTEGER NOT NULL DEFAULT 0,
    is_enabled BOOLEAN NOT NULL DEFAULT true,
    create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT now()
);

COMMENT ON TABLE leaudit_entry_modules IS '入口模块表 — 定义前端导航入口菜单及其区域配置';

COMMENT ON COLUMN leaudit_entry_modules.id IS '主键，自增';
COMMENT ON COLUMN leaudit_entry_modules.name IS '入口模块名称，唯一且非空';
COMMENT ON COLUMN leaudit_entry_modules.description IS '模块描述';
COMMENT ON COLUMN leaudit_entry_modules.path IS '前端路由路径';
COMMENT ON COLUMN leaudit_entry_modules.icon_path IS '入口菜单图标OSS路径';
COMMENT ON COLUMN leaudit_entry_modules.areas IS '区域配置JSON，格式: [{"area":"梅州","enabled":true,"sort_order":1}]';
COMMENT ON COLUMN leaudit_entry_modules.sort_order IS '排序序号';
COMMENT ON COLUMN leaudit_entry_modules.is_enabled IS '是否启用';
COMMENT ON COLUMN leaudit_entry_modules.create_time IS '创建时间';
COMMENT ON COLUMN leaudit_entry_modules.update_time IS '更新时间';


-- ============================================================================
-- 2. 新表：文档类型定义 (leaudit_document_types)
-- ============================================================================
CREATE TABLE IF NOT EXISTS leaudit_document_types (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    code VARCHAR(100) UNIQUE,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    entry_module_id BIGINT,
    classification_keywords JSONB,
    classification_tags JSONB,
    extraction_mode VARCHAR(20) NOT NULL DEFAULT 'single',
    prompt_config JSONB,
    is_enabled BOOLEAN NOT NULL DEFAULT true,
    sort_order INTEGER NOT NULL DEFAULT 0,
    create_time TIMESTAMPTZ NOT NULL DEFAULT now(),
    update_time TIMESTAMPTZ NOT NULL DEFAULT now(),
    delete_time TIMESTAMPTZ
);

COMMENT ON TABLE leaudit_document_types IS '文档类型定义表 — 定义系统支持的文档类型及其分类/抽取配置';

COMMENT ON COLUMN leaudit_document_types.id IS '主键，自增';
COMMENT ON COLUMN leaudit_document_types.code IS '文档类型编码，唯一，对应DSL的type_id（如 contract.entrust）';
COMMENT ON COLUMN leaudit_document_types.name IS '文档类型名称';
COMMENT ON COLUMN leaudit_document_types.description IS '类型描述';
COMMENT ON COLUMN leaudit_document_types.entry_module_id IS '所属入口模块ID，外键引用 leaudit_entry_modules.id';
COMMENT ON COLUMN leaudit_document_types.classification_keywords IS '分类关键词JSON，用于自动识别文档类型';
COMMENT ON COLUMN leaudit_document_types.classification_tags IS '分类标签JSON，用于前端筛选展示';
COMMENT ON COLUMN leaudit_document_types.extraction_mode IS '抽取模式: single(单实体) | multi_entity(多实体) | case_file(案卷)';
COMMENT ON COLUMN leaudit_document_types.prompt_config IS '提示词配置JSON，覆盖默认的LLM提示词模板';
COMMENT ON COLUMN leaudit_document_types.is_enabled IS '是否启用';
COMMENT ON COLUMN leaudit_document_types.sort_order IS '排序序号';
COMMENT ON COLUMN leaudit_document_types.create_time IS '创建时间';
COMMENT ON COLUMN leaudit_document_types.update_time IS '更新时间';
COMMENT ON COLUMN leaudit_document_types.delete_time IS '软删除时间，NULL表示未删除';




-- ============================================================================
-- 5. 已有表补充中文注释
-- ============================================================================

-- --------------------------------------------------------------------------
-- 5.1 jwt_tokens
-- --------------------------------------------------------------------------
COMMENT ON TABLE jwt_tokens IS 'JWT令牌表 — 管理用户登录令牌的签发、刷新、吊销全生命周期';

COMMENT ON COLUMN jwt_tokens.id IS '主键，自增';
COMMENT ON COLUMN jwt_tokens.user_id IS '用户ID，关联 sso_users.id';
COMMENT ON COLUMN jwt_tokens.token_jti IS 'JWT唯一标识符(JTI)，用于令牌吊销黑名单';
COMMENT ON COLUMN jwt_tokens.token_hash IS 'Access Token的SHA256哈希值，用于令牌有效性校验';
COMMENT ON COLUMN jwt_tokens.refresh_token_hash IS 'Refresh Token的SHA256哈希值';
COMMENT ON COLUMN jwt_tokens.token_type IS '令牌类型: ACCESS | REFRESH';
COMMENT ON COLUMN jwt_tokens.device_id IS '设备唯一标识';
COMMENT ON COLUMN jwt_tokens.device_name IS '设备名称/型号';
COMMENT ON COLUMN jwt_tokens.user_agent IS '客户端User-Agent字符串';
COMMENT ON COLUMN jwt_tokens.ip_address IS '签发时的客户端IP地址';
COMMENT ON COLUMN jwt_tokens.issued_at IS '令牌签发时间';
COMMENT ON COLUMN jwt_tokens.expires_at IS 'Access Token过期时间';
COMMENT ON COLUMN jwt_tokens.refresh_expires_at IS 'Refresh Token过期时间';
COMMENT ON COLUMN jwt_tokens.last_used_at IS '令牌最近一次使用时间';
COMMENT ON COLUMN jwt_tokens.is_revoked IS '是否已被吊销';
COMMENT ON COLUMN jwt_tokens.revoked_at IS '吊销时间';
COMMENT ON COLUMN jwt_tokens.revoke_reason IS '吊销原因: logout(主动登出) | password_change(密码修改) | admin(管理员强制) | security(安全事件)';
COMMENT ON COLUMN jwt_tokens.create_time IS '记录创建时间';
COMMENT ON COLUMN jwt_tokens.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.2 leaudit_documents
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_documents IS '文档主表 — LeAudit 平台内部文档实例，每次上传对应一条记录，可通过版本链归档历史版本';

COMMENT ON COLUMN leaudit_documents.id IS '主键，自增（leaudit内部文档ID）';
COMMENT ON COLUMN leaudit_documents.biz_document_id IS '平台内部追踪号（兼容旧字段名）';
COMMENT ON COLUMN leaudit_documents.type_id IS '文档类型ID，外键引用 leaudit_document_types.id';
COMMENT ON COLUMN leaudit_documents.processing_status IS '处理状态: waiting(等待处理) | running(处理中) | completed(已完成) | failed(失败)';
COMMENT ON COLUMN leaudit_documents.current_run_id IS '当前活跃的评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_documents.version_group_key IS '文档版本归档组键，同一名称版本链共用';
COMMENT ON COLUMN leaudit_documents.version_no IS '同一文档系列中的版本号，从1开始';
COMMENT ON COLUMN leaudit_documents.previous_version_id IS '上一版本文档ID';
COMMENT ON COLUMN leaudit_documents.root_version_id IS '首版本文档ID';
COMMENT ON COLUMN leaudit_documents.is_latest_version IS '是否当前最新版本';
COMMENT ON COLUMN leaudit_documents.normalized_name IS '归一化文件名（不含扩展名），用于版本匹配';
COMMENT ON COLUMN leaudit_documents.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_documents.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.3 leaudit_document_files
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_document_files IS '文档文件表 — 一个文档可能包含多个文件（主文件、附件、扫描件等）';

COMMENT ON COLUMN leaudit_document_files.id IS '主键，自增';
COMMENT ON COLUMN leaudit_document_files.document_id IS '所属文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_document_files.file_role IS '文件角色: primary(主文档) | attachment(附件) | scan(扫描件) | ocr_result(OCR结果)';
COMMENT ON COLUMN leaudit_document_files.file_name IS '原始文件名';
COMMENT ON COLUMN leaudit_document_files.file_ext IS '文件扩展名（不含点）';
COMMENT ON COLUMN leaudit_document_files.mime_type IS '文件MIME类型';
COMMENT ON COLUMN leaudit_document_files.file_size IS '文件大小（字节）';
COMMENT ON COLUMN leaudit_document_files.sha256 IS '文件SHA256哈希值，用于去重和完整性校验';
COMMENT ON COLUMN leaudit_document_files.local_path IS '本地临时存储路径';
COMMENT ON COLUMN leaudit_document_files.oss_url IS 'OSS对象存储URL（唯一真源）';
COMMENT ON COLUMN leaudit_document_files.storage_provider IS '存储提供商: minio | aliyun_oss | local';
COMMENT ON COLUMN leaudit_document_files.is_active IS '是否为当前活跃版本';
COMMENT ON COLUMN leaudit_document_files.created_by IS '上传者用户ID，关联 sso_users.id';
COMMENT ON COLUMN leaudit_document_files.create_time IS '记录创建时间';


-- --------------------------------------------------------------------------
-- 5.4 leaudit_audit_runs
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_audit_runs IS '评查运行表 — 一次评查执行的核心记录，跟踪7阶段流水线进度和结果汇总';

COMMENT ON COLUMN leaudit_audit_runs.id IS '主键，自增';
COMMENT ON COLUMN leaudit_audit_runs.document_id IS '被评查的文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_audit_runs.document_file_id IS '被评查的具体文件ID，外键引用 leaudit_document_files.id';
COMMENT ON COLUMN leaudit_audit_runs.run_no IS '运行序号（同一文档多次运行的递增编号）';
COMMENT ON COLUMN leaudit_audit_runs.trigger_source IS '触发来源: manual(手动) | api(API调用) | schedule(定时任务) | webhook(Webhook回调)';
COMMENT ON COLUMN leaudit_audit_runs.trigger_user_id IS '触发用户ID，外键引用 sso_users.id';
COMMENT ON COLUMN leaudit_audit_runs.task_id IS 'Celery任务ID，用于跟踪异步任务状态';
COMMENT ON COLUMN leaudit_audit_runs.status IS '运行状态: pending(等待) | running(执行中) | completed(完成) | failed(失败) | cancelled(取消)';
COMMENT ON COLUMN leaudit_audit_runs.phase IS '当前流水线阶段: normalize | extract | evaluate | rescue | persist';
COMMENT ON COLUMN leaudit_audit_runs.rule_set_id IS '使用的规则集ID，外键引用 leaudit_rule_sets.id';
COMMENT ON COLUMN leaudit_audit_runs.rule_version_id IS '使用的规则版本ID，外键引用 leaudit_rule_versions.id';
COMMENT ON COLUMN leaudit_audit_runs.rule_type_id IS '规则类型ID（DSL metadata.type_id），如 contract.entrust';
COMMENT ON COLUMN leaudit_audit_runs.rule_source_oss_url IS '规则YAML文件的OSS存储URL';
COMMENT ON COLUMN leaudit_audit_runs.rule_source_sha256 IS '规则文件SHA256哈希，用于校验规则版本一致性';
COMMENT ON COLUMN leaudit_audit_runs.rule_local_cache_path IS '规则文件本地缓存路径';
COMMENT ON COLUMN leaudit_audit_runs.engine_version IS 'LeAudit引擎版本号（pip包版本）';
COMMENT ON COLUMN leaudit_audit_runs.llm_provider IS 'LLM提供商标识';
COMMENT ON COLUMN leaudit_audit_runs.llm_model IS 'LLM模型名称';
COMMENT ON COLUMN leaudit_audit_runs.vlm_provider IS 'VLM提供商标识';
COMMENT ON COLUMN leaudit_audit_runs.vlm_model IS 'VLM模型名称';
COMMENT ON COLUMN leaudit_audit_runs.ocr_provider IS 'OCR提供商标识';
COMMENT ON COLUMN leaudit_audit_runs.ocr_model IS 'OCR模型名称';
COMMENT ON COLUMN leaudit_audit_runs.rescue_mode IS '补救模式: auto(自动) | manual(人工) | disabled(禁用)';
COMMENT ON COLUMN leaudit_audit_runs.rescue_applied IS '本次运行是否触发了补救流程';
COMMENT ON COLUMN leaudit_audit_runs.total_score IS '评查总分';
COMMENT ON COLUMN leaudit_audit_runs.passed_count IS '通过的规则数';
COMMENT ON COLUMN leaudit_audit_runs.failed_count IS '未通过的规则数';
COMMENT ON COLUMN leaudit_audit_runs.skipped_count IS '跳过的规则数（条件不满足或被禁用）';
COMMENT ON COLUMN leaudit_audit_runs.result_status IS '整体结果: pass(通过) | fail(不通过) | partial(部分通过) | review(需人工复核)';
COMMENT ON COLUMN leaudit_audit_runs.started_at IS '评查开始时间';
COMMENT ON COLUMN leaudit_audit_runs.finished_at IS '评查结束时间';
COMMENT ON COLUMN leaudit_audit_runs.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_audit_runs.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.5 leaudit_artifacts
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_artifacts IS '评查产物表 — 统一管理评查各阶段产出的所有文件/数据，OSS为真源、DB为索引';

COMMENT ON COLUMN leaudit_artifacts.id IS '主键，自增';
COMMENT ON COLUMN leaudit_artifacts.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_artifacts.document_id IS '所属文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_artifacts.artifact_type IS '产物类型: original_doc | normalized_doc | render_png | render_pdf | ocr_json | extract_json | evaluate_json | rescue_json | quality_report | diff_report | cross_review | merged_result | final_report | vlm_render | vlm_vis_page | vlm_vis_subdoc | vlm_vis_field | vlm_debug | rescue_debug | pipeline_log';
COMMENT ON COLUMN leaudit_artifacts.artifact_role IS '产物角色: input | output | intermediate | debug | log';
COMMENT ON COLUMN leaudit_artifacts.file_name IS '文件名';
COMMENT ON COLUMN leaudit_artifacts.file_ext IS '文件扩展名';
COMMENT ON COLUMN leaudit_artifacts.mime_type IS 'MIME类型';
COMMENT ON COLUMN leaudit_artifacts.file_size IS '文件大小（字节）';
COMMENT ON COLUMN leaudit_artifacts.sha256 IS '文件SHA256哈希值';
COMMENT ON COLUMN leaudit_artifacts.page_no IS '关联的页面编号（PDF渲染产物）';
COMMENT ON COLUMN leaudit_artifacts.sub_doc_id IS '关联的子文档ID（子文档级别的产物）';
COMMENT ON COLUMN leaudit_artifacts.field_name IS '关联的字段名（字段级别的可视化产物）';
COMMENT ON COLUMN leaudit_artifacts.rule_id IS '关联的规则ID（规则级别的产物）';
COMMENT ON COLUMN leaudit_artifacts.local_path IS '本地临时文件路径';
COMMENT ON COLUMN leaudit_artifacts.oss_url IS 'OSS对象存储URL（唯一真源）';
COMMENT ON COLUMN leaudit_artifacts.storage_provider IS '存储提供商: minio | aliyun_oss | local';
COMMENT ON COLUMN leaudit_artifacts.is_persisted IS '是否已持久化到OSS';
COMMENT ON COLUMN leaudit_artifacts.retention_policy IS '保留策略: temp(临时) | run_life(评查周期) | permanent(永久)';
COMMENT ON COLUMN leaudit_artifacts.create_time IS '记录创建时间';


-- --------------------------------------------------------------------------
-- 5.6 leaudit_rule_results
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_rule_results IS '规则评查结果表 — 每次评查中每条规则的判断结果，含详细评分和补救信息';

COMMENT ON COLUMN leaudit_rule_results.id IS '主键，自增';
COMMENT ON COLUMN leaudit_rule_results.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_rule_results.rule_version_id IS '使用的规则版本ID，外键引用 leaudit_rule_versions.id';
COMMENT ON COLUMN leaudit_rule_results.document_id IS '被评查文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_rule_results.rule_id IS '规则ID（DSL YAML中的 rule_id）';
COMMENT ON COLUMN leaudit_rule_results.rule_name IS '规则名称';
COMMENT ON COLUMN leaudit_rule_results.risk IS '风险等级: high | medium | low';
COMMENT ON COLUMN leaudit_rule_results.score IS '规则得分';
COMMENT ON COLUMN leaudit_rule_results.passed IS '是否通过';
COMMENT ON COLUMN leaudit_rule_results.status IS '规则状态: pending | passed | failed | skipped | error';
COMMENT ON COLUMN leaudit_rule_results.skip_reason IS '跳过原因（条件不满足时记录）';
COMMENT ON COLUMN leaudit_rule_results.confidence IS '置信度分数（0-1）';
COMMENT ON COLUMN leaudit_rule_results.pass_message IS '通过时的反馈文案';
COMMENT ON COLUMN leaudit_rule_results.fail_message IS '不通过时的反馈文案';
COMMENT ON COLUMN leaudit_rule_results.stages IS '各阶段执行详情JSON，记录每个stage的判断过程';
COMMENT ON COLUMN leaudit_rule_results.extracted_fields IS '抽取到的字段值JSON，{field_name: {value, confidence, grounding, ...}}';
COMMENT ON COLUMN leaudit_rule_results.field_positions IS '字段在文档中的位置信息JSON';
COMMENT ON COLUMN leaudit_rule_results.rule_meta IS '规则元数据JSON（risk, score, messages等快照）';
COMMENT ON COLUMN leaudit_rule_results.remediation IS '修正建议JSON，结构: {suggestions: [...], actions: [...]}';
COMMENT ON COLUMN leaudit_rule_results.rescue_applied IS '是否已应用补救流程';
COMMENT ON COLUMN leaudit_rule_results.rescue_passed IS '补救后是否通过';
COMMENT ON COLUMN leaudit_rule_results.result_payload IS '原始结果JSON（完整保留leaudit引擎输出）';
COMMENT ON COLUMN leaudit_rule_results.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_rule_results.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.7 leaudit_field_results
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_field_results IS '字段抽取结果表 — 评查中每个字段的抽取值和置信度信息';

COMMENT ON COLUMN leaudit_field_results.id IS '主键，自增';
COMMENT ON COLUMN leaudit_field_results.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_field_results.document_id IS '所属文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_field_results.sub_doc_id IS '所属子文档ID（案卷/多实体场景）';
COMMENT ON COLUMN leaudit_field_results.field_name IS '字段名（对应DSL field.name）';
COMMENT ON COLUMN leaudit_field_results.field_type IS '字段类型（对应DSL field.type，如 verbatim/string/money/date/enum）';
COMMENT ON COLUMN leaudit_field_results.value_text IS '抽取的文本值';
COMMENT ON COLUMN leaudit_field_results.raw_value_json IS '原始抽取结果JSON（含所有候选值）';
COMMENT ON COLUMN leaudit_field_results.confidence IS '综合置信度（0-1），由logprob/grounding/rule加权计算';
COMMENT ON COLUMN leaudit_field_results.logprob_score IS 'Logprob置信分（语言模型token概率）';
COMMENT ON COLUMN leaudit_field_results.grounding_score IS '文本落地置信分（抽取值在原文中的可追溯性）';
COMMENT ON COLUMN leaudit_field_results.grounding_method IS '落地验证方法: exact(精确匹配) | fuzzy(模糊匹配) | text_search(文本搜索) | none(未验证)';
COMMENT ON COLUMN leaudit_field_results.rule_score IS '规则置信分（业务规则校验）';
COMMENT ON COLUMN leaudit_field_results.hard_failed IS '是否硬失败（落在置信阈值以下且无兜底值）';
COMMENT ON COLUMN leaudit_field_results.fallback_value IS '兜底/替代值（置信不足时使用）';
COMMENT ON COLUMN leaudit_field_results.meta_json IS '扩展元数据JSON（phase信息、field位置、分组等）';
COMMENT ON COLUMN leaudit_field_results.create_time IS '记录创建时间';


-- --------------------------------------------------------------------------
-- 5.8 leaudit_run_metrics
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_run_metrics IS '评查运行指标表 — 每次运行的性能统计和耗时分解';

COMMENT ON COLUMN leaudit_run_metrics.id IS '主键，自增';
COMMENT ON COLUMN leaudit_run_metrics.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_run_metrics.ocr_seconds IS 'OCR阶段耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.normalize_seconds IS '文档归一化阶段耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.extract_seconds IS '字段抽取阶段耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.evaluate_seconds IS '规则评查阶段耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.rescue_seconds IS '补救阶段耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.total_seconds IS '总耗时（秒）';
COMMENT ON COLUMN leaudit_run_metrics.page_count IS '文档总页数';
COMMENT ON COLUMN leaudit_run_metrics.sub_document_count IS '子文档数量（案卷拆分的子文档数）';
COMMENT ON COLUMN leaudit_run_metrics.field_count IS '抽取的字段总数';
COMMENT ON COLUMN leaudit_run_metrics.rule_count IS '执行的规则总数';
COMMENT ON COLUMN leaudit_run_metrics.llm_call_count IS 'LLM调用次数';
COMMENT ON COLUMN leaudit_run_metrics.vlm_call_count IS 'VLM调用次数';
COMMENT ON COLUMN leaudit_run_metrics.rescue_rule_count IS '补救规则数（触发rescue的规则数量）';
COMMENT ON COLUMN leaudit_run_metrics.artifact_count IS '产出的产物文件总数';
COMMENT ON COLUMN leaudit_run_metrics.create_time IS '记录创建时间';


-- --------------------------------------------------------------------------
-- 5.9 leaudit_run_errors
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_run_errors IS '评查运行错误表 — 记录流水线各阶段的异常和错误详情';

COMMENT ON COLUMN leaudit_run_errors.id IS '主键，自增';
COMMENT ON COLUMN leaudit_run_errors.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_run_errors.document_id IS '所属文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_run_errors.stage IS '发生错误的阶段: ocr | normalize | extract | evaluate | rescue | persist';
COMMENT ON COLUMN leaudit_run_errors.level IS '错误级别: fatal(致命) | error(错误) | warning(警告) | info(信息)';
COMMENT ON COLUMN leaudit_run_errors.error_code IS '错误码（标准化标识，如 OCR_TIMEOUT / LLM_API_ERROR）';
COMMENT ON COLUMN leaudit_run_errors.message IS '错误描述信息';
COMMENT ON COLUMN leaudit_run_errors.detail_json IS '错误详情JSON（堆栈、上下文、请求参数等）';
COMMENT ON COLUMN leaudit_run_errors.create_time IS '记录创建时间';


-- --------------------------------------------------------------------------
-- 5.10 leaudit_rescue_outcomes
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_rescue_outcomes IS '补救结果表 — 记录每条失败规则的补救诊断和处理结果';

COMMENT ON COLUMN leaudit_rescue_outcomes.id IS '主键，自增';
COMMENT ON COLUMN leaudit_rescue_outcomes.run_id IS '所属评查运行ID，外键引用 leaudit_audit_runs.id';
COMMENT ON COLUMN leaudit_rescue_outcomes.document_id IS '所属文档ID，外键引用 leaudit_documents.id';
COMMENT ON COLUMN leaudit_rescue_outcomes.rule_id IS '被补救的规则ID（DSL rule_id）';
COMMENT ON COLUMN leaudit_rescue_outcomes.status IS '补救状态: pending | diagnosing | fixing | retrying | resolved | failed | escalated';
COMMENT ON COLUMN leaudit_rescue_outcomes.diagnosis IS '诊断结果 — AI分析出的失败根因';
COMMENT ON COLUMN leaudit_rescue_outcomes.diagnosis_confidence IS '诊断置信度（0-1）';
COMMENT ON COLUMN leaudit_rescue_outcomes.final_status IS '补救后最终状态: pass | fail | review';
COMMENT ON COLUMN leaudit_rescue_outcomes.failure_reason IS '补救失败原因（最终仍失败时记录）';
COMMENT ON COLUMN leaudit_rescue_outcomes.llm_calls IS '补救过程LLM调用次数';
COMMENT ON COLUMN leaudit_rescue_outcomes.vlm_calls IS '补救过程VLM调用次数';
COMMENT ON COLUMN leaudit_rescue_outcomes.duration_ms IS '补救总耗时（毫秒）';
COMMENT ON COLUMN leaudit_rescue_outcomes.requires_human_review IS '是否需要人工复核';
COMMENT ON COLUMN leaudit_rescue_outcomes.payload IS '补救过程完整数据JSON（含每层rescue的输入输出）';
COMMENT ON COLUMN leaudit_rescue_outcomes.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_rescue_outcomes.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.11 leaudit_rule_sets
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_rule_sets IS '规则集表 — 管理DSL规则集（一个规则集=一套type_id对应的YAML规则文件）';

COMMENT ON COLUMN leaudit_rule_sets.id IS '主键，自增';
COMMENT ON COLUMN leaudit_rule_sets.rule_type IS '规则类型ID（对应DSL metadata.type_id，如 contract.entrust）';
COMMENT ON COLUMN leaudit_rule_sets.rule_name IS '规则集名称（对应DSL metadata.name，如 通用委托合同）';
COMMENT ON COLUMN leaudit_rule_sets.domain_type IS '领域类型: contract(合同) | admin_license(行政许可) | legal_doc(法律文书)';
COMMENT ON COLUMN leaudit_rule_sets.description IS '规则集描述（对应DSL metadata.description）';
COMMENT ON COLUMN leaudit_rule_sets.entry_module IS '所属入口模块标识';
COMMENT ON COLUMN leaudit_rule_sets.current_version_id IS '当前生效版本ID，外键引用 leaudit_rule_versions.id';
COMMENT ON COLUMN leaudit_rule_sets.status IS '状态: draft(草稿) | active(生效) | deprecated(废弃) | archived(归档)';
COMMENT ON COLUMN leaudit_rule_sets.is_builtin IS '是否为系统内置规则（内置规则不可删除）';
COMMENT ON COLUMN leaudit_rule_sets.owner_user_id IS '负责人用户ID，外键引用 sso_users.id';
COMMENT ON COLUMN leaudit_rule_sets.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_rule_sets.update_time IS '记录更新时间';
COMMENT ON COLUMN leaudit_rule_sets.delete_time IS '软删除时间，NULL表示未删除';


-- --------------------------------------------------------------------------
-- 5.12 leaudit_rule_versions
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_rule_versions IS '规则版本表 — 规则集每次发布生成一个版本快照，记录YAML文件OSS地址和元数据';

COMMENT ON COLUMN leaudit_rule_versions.id IS '主键，自增';
COMMENT ON COLUMN leaudit_rule_versions.rule_set_id IS '所属规则集ID，外键引用 leaudit_rule_sets.id';
COMMENT ON COLUMN leaudit_rule_versions.version_no IS '版本号（语义化版本，如 1.0 / 2.3）';
COMMENT ON COLUMN leaudit_rule_versions.version_seq IS '版本序号（自增整数，rule_set_id内递增）';
COMMENT ON COLUMN leaudit_rule_versions.status IS '状态: draft(草稿) | published(已发布) | deprecated(废弃) | rollback(已回滚)';
COMMENT ON COLUMN leaudit_rule_versions.source_type IS '规则源类型: oss_yaml(OSS上的YAML) | local_yaml(本地YAML) | db_snippet(数据库片段)';
COMMENT ON COLUMN leaudit_rule_versions.dsl_format IS 'DSL格式: yaml(新版YAML DSL) | json(旧版JSON格式)';
COMMENT ON COLUMN leaudit_rule_versions.oss_url IS '规则YAML文件的OSS存储URL';
COMMENT ON COLUMN leaudit_rule_versions.file_sha256 IS '规则文件SHA256哈希值';
COMMENT ON COLUMN leaudit_rule_versions.file_size IS '规则文件大小（字节）';
COMMENT ON COLUMN leaudit_rule_versions.local_cache_path IS '规则文件本地缓存路径';
COMMENT ON COLUMN leaudit_rule_versions.metadata_type_id IS 'DSL metadata.type_id 快照';
COMMENT ON COLUMN leaudit_rule_versions.metadata_name IS 'DSL metadata.name 快照';
COMMENT ON COLUMN leaudit_rule_versions.metadata_version IS 'DSL metadata.version 快照';
COMMENT ON COLUMN leaudit_rule_versions.change_note IS '版本变更说明';
COMMENT ON COLUMN leaudit_rule_versions.editor_user_id IS '编辑者用户ID，外键引用 sso_users.id';
COMMENT ON COLUMN leaudit_rule_versions.publisher_user_id IS '发布者用户ID，外键引用 sso_users.id';
COMMENT ON COLUMN leaudit_rule_versions.published_at IS '发布时间';
COMMENT ON COLUMN leaudit_rule_versions.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_rule_versions.update_time IS '记录更新时间';


-- --------------------------------------------------------------------------
-- 5.13 leaudit_rule_type_bindings
-- --------------------------------------------------------------------------
COMMENT ON TABLE leaudit_rule_type_bindings IS '规则类型绑定表 — 定义文档类型与规则集的匹配关系，支持显式绑定和通配符绑定';

COMMENT ON COLUMN leaudit_rule_type_bindings.id IS '主键，自增';
COMMENT ON COLUMN leaudit_rule_type_bindings.doc_type_id IS '文档类型ID，外键引用 leaudit_document_types.id';
COMMENT ON COLUMN leaudit_rule_type_bindings.doc_type_code IS '文档类型编码（冗余字段，用于快速匹配）';
COMMENT ON COLUMN leaudit_rule_type_bindings.rule_set_id IS '规则集ID，外键引用 leaudit_rule_sets.id';
COMMENT ON COLUMN leaudit_rule_type_bindings.binding_mode IS '绑定模式: explicit(显式指定) | wildcard(通配符) | fallback(兜底默认)';
COMMENT ON COLUMN leaudit_rule_type_bindings.priority IS '优先级（数值越大优先级越高），通配符绑定通常优先级较低';
COMMENT ON COLUMN leaudit_rule_type_bindings.is_active IS '是否激活';
COMMENT ON COLUMN leaudit_rule_type_bindings.note IS '备注说明';
COMMENT ON COLUMN leaudit_rule_type_bindings.create_time IS '记录创建时间';
COMMENT ON COLUMN leaudit_rule_type_bindings.update_time IS '记录更新时间';


-- ============================================================================
-- 6. 外键约束 (新表)
-- ============================================================================

    ADD CONSTRAINT fk_eval_points_rule_set
    FOREIGN KEY (rule_set_id) REFERENCES leaudit_rule_sets(id)
    ON UPDATE CASCADE ON DELETE SET NULL;

-- leaudit_document_types → leaudit_entry_modules
ALTER TABLE leaudit_document_types
    ADD CONSTRAINT fk_doc_types_entry_module
    FOREIGN KEY (entry_module_id) REFERENCES leaudit_entry_modules(id)
    ON UPDATE CASCADE ON DELETE SET NULL;

-- leaudit_rule_type_bindings → leaudit_document_types
ALTER TABLE leaudit_rule_type_bindings
    ADD CONSTRAINT fk_rule_type_bindings_doc_type
    FOREIGN KEY (doc_type_id) REFERENCES leaudit_document_types(id)
    ON UPDATE CASCADE ON DELETE CASCADE;

-- leaudit_documents → leaudit_document_types
ALTER TABLE leaudit_documents
    ADD CONSTRAINT fk_documents_type
    FOREIGN KEY (type_id) REFERENCES leaudit_document_types(id)
    ON UPDATE CASCADE ON DELETE SET NULL;


-- ============================================================================
-- 7. 索引
-- ============================================================================

-- 入口模块
CREATE INDEX IF NOT EXISTS idx_entry_modules_sort ON leaudit_entry_modules(sort_order, is_enabled);

-- 文档类型
CREATE INDEX IF NOT EXISTS idx_doc_types_code ON leaudit_document_types(code);
CREATE INDEX IF NOT EXISTS idx_doc_types_entry ON leaudit_document_types(entry_module_id);


COMMIT;
