-- Migration 026: create audit_log table for data-level audit trail
-- Records before/after snapshots of every row mutation on core tables.
-- Admin actions are already logged separately via admin_audit_log.
CREATE TABLE IF NOT EXISTS audit_log (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp   TEXT    NOT NULL DEFAULT (datetime('now')),
    actor       TEXT    NOT NULL,
    action      TEXT    NOT NULL CHECK(action IN ('INSERT','UPDATE','DELETE')),
    table_name  TEXT    NOT NULL,
    record_id   INTEGER,
    old_data    TEXT,
    new_data    TEXT
);

CREATE INDEX IF NOT EXISTS idx_audit_log_table_record ON audit_log(table_name, record_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_timestamp      ON audit_log(timestamp);
