CREATE TABLE IF NOT EXISTS rm_admin_action_audit (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id UUID REFERENCES rm_users(id) ON DELETE SET NULL,
  user_email VARCHAR(255),
  area VARCHAR(100) NOT NULL,
  action VARCHAR(255) NOT NULL,
  allowed BOOLEAN NOT NULL DEFAULT TRUE,
  status_code INTEGER NOT NULL,
  ip_address VARCHAR(100),
  user_agent VARCHAR(512),
  metadata JSONB,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_rm_admin_action_audit_created_at
  ON rm_admin_action_audit(created_at DESC);

CREATE INDEX IF NOT EXISTS idx_rm_admin_action_audit_user_id
  ON rm_admin_action_audit(user_id, created_at DESC);
