-- Athena Auth: performance indexes for email admin/audit multi-tenant operations

-- Fast resource-oriented audit lookups (e.g. by target type + id)
CREATE INDEX IF NOT EXISTS idx_admin_audit_logs_target
ON admin_audit_logs (target_type, target_id);

-- Common admin filter shape: success/action recency
CREATE INDEX IF NOT EXISTS idx_admin_audit_logs_success_action_created
ON admin_audit_logs (success, action, created_at DESC);

-- Metadata filtering support for JSONB audit payloads
CREATE INDEX IF NOT EXISTS idx_admin_audit_logs_metadata_gin
ON admin_audit_logs
USING GIN (metadata);

-- Common email/failure list sort/filter combinations
CREATE INDEX IF NOT EXISTS idx_email_send_failures_resolved_created_at
ON email_send_failures (resolved, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_emails_flow_created_at
ON emails (flow, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_email_templates_key_locale_active
ON email_templates (template_key, locale, is_active);
