-- 175_scrape_schedules_seed_domclick_detail_backfill.sql
-- Nightly detail-enrichment backfill (Layer B) for domklik listings (issue #2000).
--
-- Root cause (#2000):
--   DomClick SERP layer (Layer A) discovers listings via the BFF JSON API but does not
--   fetch card-detail pages (repair_state, living/kitchen area, year_built, owners_count,
--   price history). Detail-enrichment requires a per-card browser-fetch that is
--   QRATOR-guarded -- solved at the single-fetch level by PR #2430 (organic same-site
--   SERP-origin navigation) + PR #2433 (cookie-injection MVP, authenticated test-account
--   session bypasses the QRATOR reputation-block even on an already-suspicious proxy-IP).
--   This schedule wires that session->cookies->fetch_detail path into the production
--   scheduled orchestrator (app.tasks.domclick_detail_backfill), previously only
--   reachable via the manual debug endpoint (POST /scrape/domclick/debug/detail-fetch).
--
-- Solution:
--   Nightly backfill task (domclick_detail_backfill) takes a SNAPSHOT of pending
--   listings (single SELECT LIMIT batch_size at start, source='domklik' -- see NAMING
--   TRAP note in app/tasks/domclick_detail_backfill.py module docstring), then iterates
--   with budget guard through ONE BrowserFetcher(source="domclick") per run with cookies
--   loaded once via domclick_session.load_session(). DomClickBlockedError (QRATOR
--   challenge) increments a consecutive-block counter, aborting the run (mark_done, NOT
--   mark_failed -- block is an expected operational outcome) once max_consecutive_blocks
--   is hit. DomClickParseError (schema drift) is a plain failed++, does not abort.
--
-- Window 15:00-18:00 UTC (18:00-21:00 MSK):
--   - Offset from avito_detail_backfill (09-12 UTC) and yandex_detail_backfill (12-15
--     UTC) -- avoids simultaneous egress across scraper sources sharing the
--     tradein-browser pool.
--
-- default_params (more conservative than Avito -- DomClick is QRATOR
-- reputation-based, single dedicated residential proxy, no IP-rotation recovery path
-- on block; a burned session/proxy reputation is costlier to recover than an Avito
-- IP-block, which can rotate):
--   batch_size  -- 200 (vs Avito's 800): smaller batch keeps a bad run's blast radius
--                  small while the pipeline is still ramping up post-#2000.
--   budget_sec  -- 3600 = 1 hour (same as Avito, fits well within the 3h window).
--   request_delay_sec -- 12s (vs Avito's 6s): slower organic pacing matches the
--                  same-site SERP-origin navigation cadence from PR #2430 -- QRATOR is
--                  reputation-based, not purely rate-based, but a more human cadence
--                  reduces the odds of tripping it in the first place.
--   max_consecutive_blocks -- 3 (vs Avito's 5): DomClick has no IP-rotation recovery
--                  on block (one dedicated residential proxy, not a pool) -- aborting
--                  sooner avoids hammering an already-reputation-damaged proxy/session
--                  and preserves it for the next scheduled window.
--
-- Seeded with enabled=false: activation is a deliberate, separate manual step after
-- this migration deploys and the orchestrator is smoke-tested against a live cookie
-- session (see app/services/domclick_session.py -- MVP requires a manually uploaded
-- test-account session, no auto-login).
--
-- DEPENDENCIES: 052_scrape_schedules.sql (table + UNIQUE(source)),
--   173_scrape_proxies_add_domclick_affinity.sql (source="domclick" proxy pool),
--   174_domclick_session_cookies.sql (cookie storage).
-- Idempotent: ON CONFLICT (source) DO NOTHING.

BEGIN;

INSERT INTO scrape_schedules (
    source,
    enabled,
    window_start_hour,
    window_end_hour,
    next_run_at,
    default_params
)
VALUES
(
    'domclick_detail_backfill',
    false,
    15,
    18,
    ((CURRENT_DATE + INTERVAL '1 day') + make_interval(hours => 15)) AT TIME ZONE 'UTC',
    '{"batch_size": 200, "budget_sec": 3600, "request_delay_sec": 12, "max_consecutive_blocks": 3}'::jsonb
)
ON CONFLICT (source) DO NOTHING;

COMMENT ON TABLE scrape_schedules IS
    'In-app scheduler config (replaces cron-script setup). Sources: avito_city_sweep, yandex_city_sweep (dormant, #561), cian_history_backfill, rosreestr_dkp_import, listing_source_snapshot (#570), asking_to_sold_ratio_refresh (#648), refresh_search_matview (#769), yandex_address_backfill (#855, EKB pilot), sber_index_pull (#887, monthly), rosreestr_quarter_poll (#888, monthly), cian_city_sweep (dormant, #973), yandex_newbuilding_sweep (dormant, #974), geocode_missing_listings (#1: listings geom backfill, all sources), avito_detail_backfill (#1551: nightly detail-enrichment backfill for legacy avito listings), domclick_detail_backfill (#2000: nightly Layer B detail-enrichment backfill for domklik listings, cookie-injection + QRATOR-aware, disabled by default until smoke-tested).';

COMMIT;
