-- 091_scrape_schedules_seed_yandex_address_backfill.sql
-- Issue #855 — seed scrape_schedules row for nightly yandex address backfill.
--
-- Context:
--   Active EKB yandex listings often have street-only addresses (e.g. «улица Горького»)
--   because Yandex SERP cards omit the house number. The detail page <title> contains
--   the full address including house number:
--     «Продажа квартиры — Екатеринбург, улица Горького, 36 — id 7654321».
--   Without house numbers, geocoding falls back to street-centroid coordinates and
--   valuation analogs have lower match quality.
--
--   Fix:
--     1. services/yandex_address_backfill.py — backfill_yandex_addresses() service function
--        (curl_cffi chrome120, RE_TITLE_ADDRESS regex over <title>, UPDATE only-if-changed).
--     2. tasks/yandex_address_backfill.py — run_yandex_address_backfill() task wrapper
--        (run lifecycle: heartbeat + mark_done/mark_failed).
--     3. scheduler.py — trigger_yandex_address_backfill_run() + dispatch branch
--        `elif source == "yandex_address_backfill"` in scheduler_loop().
--     4. This migration — seeds the scrape_schedules row (enabled=true, 02:00-03:00 UTC).
--
-- Schedule window 02:00-03:00 UTC:
--   Nightly, before refresh_search_matview (03:00-04:00 UTC). Runs after the yandex
--   city sweep (dormant/manual window 01:00-02:00 UTC) so backfill sees the freshest
--   is_active=TRUE rows. Expected duration: ~30 min for 200 listings at 3s delay.
--
-- next_run_at bootstrapped to tomorrow 02:00 UTC so the scheduler does not fire
-- immediately on deploy (same pattern as 078/079/082/088).
--
-- Idempotent: ON CONFLICT (source) DO NOTHING — safe to re-apply.
--
-- Dependencies:
--   052_scrape_schedules.sql (table + UNIQUE(source)).
--   scheduler.py change (trigger_yandex_address_backfill_run must be deployed).
--   services/yandex_address_backfill.py (backfill_yandex_addresses must be deployed).
--
-- Deploy order:
--   Apply this migration after deploying the scheduler.py + task changes so the
--   scheduler can dispatch the new source name correctly on first fire.
--
-- EKB pilot scope: only listings with region_code=66 are processed. Expanding to
--   other cities requires parametrizing the city/region_code in backfill_yandex_addresses.

BEGIN;

INSERT INTO scrape_schedules (
    source,
    enabled,
    window_start_hour,
    window_end_hour,
    next_run_at,
    default_params
)
VALUES
(
    'yandex_address_backfill',
    true,                                        -- EKB pilot; HTTP calls (curl_cffi chrome120)
    2,                                           -- window 02:00-03:00 UTC
    3,
    ((CURRENT_DATE + INTERVAL '1 day') + make_interval(hours => 2)) AT TIME ZONE 'UTC',
    '{"batch_size": 200, "request_delay_sec": 3.0}'::jsonb
)
ON CONFLICT (source) DO NOTHING;

COMMENT ON TABLE scrape_schedules IS
    'In-app scheduler config (заменяет 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).';

COMMIT;
