-- 006_audit_logs.sql
CREATE TYPE audit_actor_type AS ENUM ('user', 'system');

CREATE TABLE audit_logs (
  id          UUID             PRIMARY KEY DEFAULT gen_random_uuid(),
  actor_id    UUID             REFERENCES users(id),
  actor_type  audit_actor_type NOT NULL,
  action      VARCHAR(100)     NOT NULL,
  entity_type VARCHAR(50)      NOT NULL,
  entity_id   UUID             NOT NULL,
  metadata    JSONB,
  created_at  TIMESTAMPTZ      NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_audit_actor     ON audit_logs (actor_id, created_at);
CREATE INDEX idx_audit_entity    ON audit_logs (entity_type, entity_id);
CREATE INDEX idx_audit_action    ON audit_logs (action, created_at);

-- Audit logs are immutable — enforce via policy / application logic
