-- Migration 080: Performance indexes for User Insights dashboard
-- Addresses slow queries in _tab_problems and _tab_engagement

-- audit_logs: user_email + action + created_at (problem score, failed logins)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_al_email_action_date
    ON audit_logs(user_email, action, created_at DESC);

-- email_logs: recipient_email + email_type + created_at (password resets)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_el_recipient_type_date
    ON email_logs(recipient_email, email_type, created_at DESC);

-- Grant permissions
GRANT ALL ON TABLE audit_logs TO nordabiz_app;
GRANT ALL ON TABLE email_logs TO nordabiz_app;
