-- 官方导入结构签名审计

CREATE TABLE IF NOT EXISTS official_import_signature_audit (
    id BIGSERIAL PRIMARY KEY,
    source_key TEXT NOT NULL,
    checked_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    status TEXT NOT NULL,
    drift_detected BOOLEAN NOT NULL DEFAULT FALSE,
    baseline_initialized BOOLEAN NOT NULL DEFAULT FALSE,
    source_url TEXT,
    fixture_path TEXT,
    snapshot_path TEXT,
    signature_path TEXT,
    baseline_path TEXT,
    structure_sha256 TEXT,
    previous_structure_sha256 TEXT,
    byte_size INTEGER,
    signature_payload JSONB,
    error_message TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_official_import_signature_audit_source_checked_at
    ON official_import_signature_audit(source_key, checked_at DESC);
CREATE INDEX IF NOT EXISTS idx_official_import_signature_audit_status
    ON official_import_signature_audit(status);
CREATE INDEX IF NOT EXISTS idx_official_import_signature_audit_structure_sha256
    ON official_import_signature_audit(structure_sha256);

COMMENT ON TABLE official_import_signature_audit IS '官方导入结构签名巡检审计表，记录每次 guard 抓取、签名与漂移判定结果';
COMMENT ON COLUMN official_import_signature_audit.signature_payload IS '当前抓取页面的结构签名 JSONB 快照';
