CREATE INDEX IF NOT EXISTS idx_collect_tasks_global_lease_expired
    ON monitoring_collect_tasks (lease_expires_at, id)
    INCLUDE (tenant_id)
    WHERE collector_type = 'desktop'
      AND status = 'leased'
      AND lease_expires_at IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_collect_tasks_result_recovery_queue
    ON monitoring_collect_tasks (callback_received_at, id)
    WHERE collector_type = 'desktop'
      AND callback_received_at IS NOT NULL
      AND request_payload_json IS NOT NULL
      AND jsonb_typeof(request_payload_json) = 'object'
      AND (
          status = 'received'
          OR (
              COALESCE(execution_owner, 'legacy') = 'desktop_tasks'
              AND status = 'pending'
          )
      );

CREATE INDEX IF NOT EXISTS idx_collect_tasks_received_watchdog
    ON monitoring_collect_tasks (callback_received_at, id)
    WHERE collector_type = 'desktop'
      AND callback_received_at IS NOT NULL
      AND (
          status = 'received'
          OR (
              COALESCE(execution_owner, 'legacy') = 'desktop_tasks'
              AND status = 'pending'
          )
      )
      AND (
          request_payload_json IS NULL
          OR jsonb_typeof(request_payload_json) <> 'object'
          OR NOT (request_payload_json ? 'received_alerted_at')
      );

CREATE INDEX IF NOT EXISTS idx_collect_tasks_brand_day_existing
    ON monitoring_collect_tasks (tenant_id, brand_id, business_date, question_id, ai_platform_id)
    WHERE collector_type = 'desktop'
      AND run_mode = 'desktop_standard';

CREATE INDEX IF NOT EXISTS idx_collect_tasks_brand_day_materialized
    ON monitoring_collect_tasks (tenant_id, brand_id, business_date)
    WHERE collector_type = 'desktop'
      AND trigger_source = 'automatic'
      AND run_mode = 'desktop_standard'
      AND COALESCE(execution_owner, 'legacy') = 'desktop_tasks';

CREATE INDEX IF NOT EXISTS idx_collect_tasks_daily_dispatch_due
    ON monitoring_collect_tasks (
        tenant_id,
        brand_id,
        business_date,
        dispatch_after,
        last_dispatched_at,
        dispatch_priority DESC,
        id
    )
    WHERE collector_type = 'desktop'
      AND status = 'pending'
      AND callback_received_at IS NULL
      AND trigger_source = 'automatic'
      AND run_mode = 'desktop_standard'
      AND COALESCE(execution_owner, 'legacy') = 'desktop_tasks';

CREATE INDEX IF NOT EXISTS idx_collect_dispatch_outbox_retention
    ON monitoring_collect_dispatch_outbox (created_at, id);

CREATE INDEX IF NOT EXISTS idx_collect_requests_retention
    ON monitoring_collect_requests (created_at, id);

CREATE INDEX IF NOT EXISTS idx_monitoring_brand_platform_daily_retention
    ON monitoring_brand_platform_daily (business_date, id);

CREATE INDEX IF NOT EXISTS idx_monitoring_brand_daily_retention
    ON monitoring_brand_daily (business_date, id);

CREATE INDEX IF NOT EXISTS idx_platform_access_snapshots_retention
    ON monitoring_platform_access_snapshots (business_date, id);

CREATE INDEX IF NOT EXISTS idx_collect_tasks_retention
    ON monitoring_collect_tasks (business_date, id);

CREATE INDEX IF NOT EXISTS idx_question_monitor_runs_retention
    ON question_monitor_runs (business_date, id);
