-- ============================================================
-- Fabric audit event sink — the table Pikku Fabric writes AuditEvent
-- records to. This is the framework-level event stream (distinct from
-- any domain-level change history).
-- Columns mirror the AuditEvent shape (camelCase → snake_case for the
-- CamelCasePlugin kysely).
-- ============================================================

CREATE TABLE IF NOT EXISTS audit (
  event_id TEXT PRIMARY KEY,
  type TEXT NOT NULL,
  source TEXT,
  outcome TEXT,
  occurred_at TEXT NOT NULL DEFAULT (datetime('now')),
  function_id TEXT,
  wire_type TEXT,
  wire_id TEXT,
  trace_id TEXT,
  transaction_id TEXT,
  query_id TEXT,
  actor TEXT,
  input TEXT,
  metadata TEXT
);

CREATE INDEX IF NOT EXISTS idx_audit_type ON audit(type);
CREATE INDEX IF NOT EXISTS idx_audit_occurred ON audit(occurred_at);
CREATE INDEX IF NOT EXISTS idx_audit_function ON audit(function_id);
CREATE INDEX IF NOT EXISTS idx_audit_trace ON audit(trace_id);
