CREATE INDEX IF NOT EXISTS idx_schedule_task_missing_next_run
    ON schedule_tasks (created_at, id)
    WHERE deleted_at IS NULL
      AND status = 'enabled'
      AND next_run_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_schedule_task_due_dispatch
    ON schedule_tasks (next_run_at, id)
    WHERE deleted_at IS NULL
      AND status = 'enabled'
      AND next_run_at IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_desktop_clients_active_plan_candidate
    ON desktop_clients (tenant_id, workspace_id, created_at, id)
    INCLUDE (last_seen_at)
    WHERE revoked_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_platform_accounts_monitoring_client_platform
    ON platform_accounts (tenant_id, workspace_id, client_id, platform_id)
    WHERE deleted_at IS NULL
      AND client_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_brands_monitoring_daily_candidate
    ON brands (tenant_id, created_at, id)
    WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_tenant_plan_active_window
    ON tenant_plan_subscriptions (tenant_id, start_at, end_at, plan_id)
    WHERE status = 'active'
      AND deleted_at IS NULL;
