-- Migration: micro_optimisation_indexes
-- Date: 2026-01-22
-- Description: Add missing indexes for common query patterns to improve performance

-- Index for assigned_to lookups (call list filtering by rep)
CREATE INDEX IF NOT EXISTS idx_leads_assigned_to ON leads(assigned_to);

-- Index for created_at range queries (time-based filtering)
CREATE INDEX IF NOT EXISTS idx_leads_created_at ON leads(created_at DESC);

-- Index for last_contact_at range queries (stale lead detection)
CREATE INDEX IF NOT EXISTS idx_leads_last_contact_at ON leads(last_contact_at DESC);

-- Index for communications created_at (timeline queries)
CREATE INDEX IF NOT EXISTS idx_communications_created_at ON communications(created_at DESC);

-- Index for handled_by lookups (rep performance queries)
CREATE INDEX IF NOT EXISTS idx_communications_handled_by ON communications(handled_by);

-- Composite index for callbacks status + scheduled_at (pending callback queries)
CREATE INDEX IF NOT EXISTS idx_callbacks_status_scheduled ON callbacks(status, scheduled_at);

-- Index for callbacks scheduled_at (timeline ordering)
CREATE INDEX IF NOT EXISTS idx_callbacks_scheduled_at ON callbacks(scheduled_at DESC);
