-- 112_scrape_schedules_seed_avito_detail_backfill.sql
-- Nightly detail-enrichment backfill for legacy avito listings (detail_enriched_at IS NULL).
--
-- Root cause (#1551):
--   City sweep ENRICH_DETAIL step only enriches top-N listings proximate to each anchor.
--   Legacy avito listings (older than 2h or outside radius) are never enriched.
--   On prod: 15243/15917 avito listings with detail_enriched_at IS NULL.
--   These carry no kitchen_area_m2 / ceiling_height_m / finishing / views / description.
--
-- Solution:
--   Nightly backfill task (avito_detail_backfill) takes a SNAPSHOT of pending listings
--   (single SELECT LIMIT batch_size at start, same proxy path as scrape_pipeline step 5),
--   then iterates with budget guard. Block handling: rotate IP on every AvitoBlockedError,
--   abort after max_consecutive_blocks (mark_done, NOT mark_failed -- block is temporary).
--
-- Window 09:00-12:00 UTC (12:00-15:00 MSK):
--   - After city sweeps (~03-04 UTC, depositing new listings).
--   - After geocode_missing_listings backfill (06:00-09:00 UTC).
--   - Offset from avito_city_sweep to avoid sharing proxy IP simultaneously.
--
-- default_params:
--   batch_size  -- listings per run (800 ~ 1.3h at 6s/listing, fits in 3h window).
--   budget_sec  -- wall-clock budget; 3600 = 1 hour (safe within 3h window).
--   request_delay_sec -- anti-bot delay between requests (6s = ~600 req/hour).
--   max_consecutive_blocks -- abort threshold for consecutive IP blocks (5).
--
-- DEPENDENCIES: 052_scrape_schedules.sql (table + UNIQUE(source)).
-- 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
(
    'avito_detail_backfill',
    true,
    9,
    12,
    ((CURRENT_DATE + INTERVAL '1 day') + make_interval(hours => 9)) AT TIME ZONE 'UTC',
    '{"batch_size": 800, "budget_sec": 3600, "request_delay_sec": 6, "max_consecutive_blocks": 5}'::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).';

COMMIT;
