-- ================================================================
-- 创建 metadata_review_records 表脚本
-- 用于存储元数据疑似冗余/变动审核记录（PG工作流表）
-- ================================================================

CREATE TABLE IF NOT EXISTS public.metadata_review_records (
    id BIGSERIAL PRIMARY KEY,
    record_type VARCHAR(20) NOT NULL, -- redundancy | change
    source VARCHAR(50) NOT NULL DEFAULT 'ddl',
    business_domain_id BIGINT,

    -- 新解析出来的元数据快照
    new_meta JSONB NOT NULL,

    -- 疑似重复候选列表（redundancy 用）
    candidates JSONB NOT NULL DEFAULT '[]'::jsonb,

    -- 变动前快照（change 用）
    old_meta JSONB,

    status VARCHAR(20) NOT NULL DEFAULT 'pending', -- pending | resolved | ignored
    resolution_action VARCHAR(30), -- alias | create_new | accept_change | reject_change | ignore
    resolution_payload JSONB,

    notes TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    resolved_at TIMESTAMP,
    resolved_by VARCHAR(100)
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_metadata_review_records_status
    ON public.metadata_review_records(status);
CREATE INDEX IF NOT EXISTS idx_metadata_review_records_type
    ON public.metadata_review_records(record_type);
CREATE INDEX IF NOT EXISTS idx_metadata_review_records_bd_id
    ON public.metadata_review_records(business_domain_id);
CREATE INDEX IF NOT EXISTS idx_metadata_review_records_created_at
    ON public.metadata_review_records(created_at DESC);

-- 注释
COMMENT ON TABLE public.metadata_review_records IS '元数据疑似冗余/变动审核记录表（前端审核流工作台）';
COMMENT ON COLUMN public.metadata_review_records.record_type IS '记录类型：redundancy(疑似重复)/change(疑似变动)';
COMMENT ON COLUMN public.metadata_review_records.source IS '触发来源：ddl等';
COMMENT ON COLUMN public.metadata_review_records.business_domain_id IS '触发记录的业务领域Neo4j节点ID';
COMMENT ON COLUMN public.metadata_review_records.new_meta IS '新解析出来的字段元数据快照（jsonb）';
COMMENT ON COLUMN public.metadata_review_records.candidates IS '疑似重复候选列表（jsonb）';
COMMENT ON COLUMN public.metadata_review_records.old_meta IS '变动前快照（jsonb）';
COMMENT ON COLUMN public.metadata_review_records.status IS '处理状态：pending/resolved/ignored';
COMMENT ON COLUMN public.metadata_review_records.resolution_action IS '处理动作：alias/create_new/accept_change/reject_change/ignore';
COMMENT ON COLUMN public.metadata_review_records.resolution_payload IS '处理动作参数（jsonb）';
COMMENT ON COLUMN public.metadata_review_records.notes IS '备注';
COMMENT ON COLUMN public.metadata_review_records.resolved_by IS '处理人';

-- 更新时间触发器
CREATE OR REPLACE FUNCTION public.update_metadata_review_records_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trigger_update_metadata_review_records_updated_at
    ON public.metadata_review_records;
CREATE TRIGGER trigger_update_metadata_review_records_updated_at
    BEFORE UPDATE ON public.metadata_review_records
    FOR EACH ROW
    EXECUTE FUNCTION public.update_metadata_review_records_updated_at();


