CREATE TABLE IF NOT EXISTS access_log (
  id         SERIAL PRIMARY KEY,
  user_email TEXT        NOT NULL,
  action     TEXT        NOT NULL CHECK (action IN ('login', 'view', 'download')),
  detail     TEXT,
  ip         TEXT,
  logged_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS access_log_logged ON access_log (logged_at DESC);
