-- Admin console support: manual edit provenance + audit log.

-- Manual-entry source used to record field-level provenance for operator edits.
INSERT INTO source (name, homepage, license, trust_weight, notes)
VALUES ('manual', NULL, 'proprietary', 0.90, '运营人工录入/补全')
ON CONFLICT (name) DO NOTHING;

-- Audit trail: every admin write records actor, action, entity and before/after.
CREATE TABLE IF NOT EXISTS audit_log (
    id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    actor      TEXT NOT NULL,
    action     TEXT NOT NULL,
    entity     TEXT NOT NULL,
    entity_id  UUID,
    fields     TEXT[] NOT NULL DEFAULT '{}',
    before     JSONB,
    after      JSONB,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_audit_entity ON audit_log (entity, entity_id);
CREATE INDEX IF NOT EXISTS idx_audit_created ON audit_log (created_at DESC);
