-- Migration 105: Add indexes for dashboard performance
-- Purpose: Speed up the /dashboard and /national/summary endpoints
--
-- These queries were identified as slow:
-- 1. COUNT(*) FROM store_product_snapshots WHERE captured_at >= NOW() - INTERVAL '24 hours'
-- 2. National summary aggregate queries

-- Index for snapshot counts by time (used in dashboard)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_store_product_snapshots_captured_at
ON store_product_snapshots(captured_at DESC);

-- Index for crawl traces by time and success (used in dashboard)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_crawl_traces_started_success
ON crawl_orchestration_traces(started_at DESC, success);

-- Partial index for recent failed crawls (faster for dashboard alerts)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_crawl_traces_recent_failures
ON crawl_orchestration_traces(started_at DESC)
WHERE success = false;

-- Composite index for store_products aggregations by dispensary
-- Helps with national summary state metrics query
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_store_products_dispensary_brand
ON store_products(dispensary_id, brand_name_raw)
WHERE brand_name_raw IS NOT NULL;
