"""Add missing indexes identified in performance review.

Columns used in WHERE clauses across admin and worker routes that
lacked indexes and would cause full table scans as the tables grow.
"""


def up(conn) -> None:
    # lead_requests: filtered by lead_type, status, verified_at, country
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_lead_requests_lead_type"
        " ON lead_requests(lead_type)"
    )
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_lead_requests_type_status"
        " ON lead_requests(lead_type, status)"
    )
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_lead_requests_type_verified"
        " ON lead_requests(lead_type, verified_at)"
    )
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_lead_requests_country"
        " ON lead_requests(country)"
    )

    # suppliers: filtered by tier and claimed_by in admin list + worker refill
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_suppliers_tier"
        " ON suppliers(tier)"
    )
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_suppliers_claimed_by"
        " ON suppliers(claimed_by)"
    )

    # credit_ledger: balance queries sum delta grouped by supplier_id
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_credit_ledger_supplier"
        " ON credit_ledger(supplier_id)"
    )
