-- ============================================================
-- 表12：audit_log_national（审计日志 · 国家操作 · append-only）
-- 用途：国家系统级别的操作审计，完全透明，不可篡改
-- Hash chain：每行包含上一行的checksum形成链式结构
-- ============================================================

CREATE TABLE IF NOT EXISTS audit_log_national (
  log_id            INTEGER       PRIMARY KEY AUTOINCREMENT,
  operation         VARCHAR(64)   NOT NULL,
  actor             VARCHAR(64)   NOT NULL,
  target_type       VARCHAR(32),
  target_id         VARCHAR(64),
  details           TEXT,                          -- JSON object
  ip_address        VARCHAR(45),
  result            VARCHAR(16)   NOT NULL
                    CHECK (result IN ('success', 'failure', 'pending')),
  checksum          VARCHAR(128),                   -- hash chain for append-only integrity
  prev_log_id       INTEGER       REFERENCES audit_log_national(log_id),
  timestamp         TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_aln_operation ON audit_log_national(operation);
CREATE INDEX IF NOT EXISTS idx_aln_actor ON audit_log_national(actor);
CREATE INDEX IF NOT EXISTS idx_aln_timestamp ON audit_log_national(timestamp);
