-- 093_scrape_schedules_seed_sber_index_pull.sql
-- Issue #887 — seed scrape_schedules row for monthly СберИндекс price-index pull.
--
-- Context:
--   app/services/sber_index.py fetches city-level secondary-housing price statistics
--   from sberindex.ru/api/sowa (three dashboards: real_estate_deals,
--   residential_real_estate_prices, dinamika-tsen-obyavlenii) and upserts rows into
--   sber_price_index (created in migration 092).
--
--   The pull is a pure HTTP + DB operation — no scraper proxy, no anti-bot, no cookies.
--   The /api/sowa endpoint requires only the rquid header (32-hex UUID) and a
--   content-type: application/json header. Rate is low (3 series × 3 cities = 9 requests
--   per run, monthly data since 2017 — ~100 rows per series per city at most).
--
--   Fix:
--     1. services/sber_index.py — fetch_sber_index() + pull_sber_indices() service.
--     2. tasks/sber_index_pull.py — run_sber_index_pull() task wrapper.
--     3. scheduler.py — trigger_sber_index_pull_run() + dispatch branch
--        `elif source == "sber_index_pull"`.
--     4. This migration — seeds the scrape_schedules row (enabled=true, monthly window).
--
-- Schedule window 05:00-06:00 UTC:
--   Monthly cadence — runs roughly once a month. Placed after rosreestr_dkp_import
--   (04:00-06:00 UTC window) so both windows overlap in the early-morning batch slot.
--   The index is updated by СберИндекс once a month; daily pulls would be wasteful.
--   default_params.interval_days=28 mirrors the avito / yandex monthly approach
--   (scheduler re-computes next_run_at after each fire; 28 days ≈ monthly).
--
-- next_run_at bootstrapped to tomorrow 05:00 UTC — scheduler will not fire immediately
-- on deploy (same pattern as 078/079/082/088/091).
--
-- Idempotent: ON CONFLICT (source) DO NOTHING — safe to re-apply.
--
-- Dependencies:
--   052_scrape_schedules.sql (table + UNIQUE(source)).
--   092_sber_price_index.sql (sber_price_index table must exist).
--   scheduler.py change (trigger_sber_index_pull_run must be deployed).
--   services/sber_index.py + tasks/sber_index_pull.py must be deployed.
--
-- Deploy order:
--   Apply after deploying the scheduler.py + task + service changes so the scheduler
--   can dispatch 'sber_index_pull' correctly on first fire.

BEGIN;

INSERT INTO scrape_schedules (
    source,
    enabled,
    window_start_hour,
    window_end_hour,
    next_run_at,
    default_params
)
VALUES
(
    'sber_index_pull',
    true,                                        -- SAFE: pure HTTP+DB, no anti-bot, low rate
    5,                                           -- window 05:00-06:00 UTC
    6,
    ((CURRENT_DATE + INTERVAL '1 day') + make_interval(hours => 5)) AT TIME ZONE 'UTC',
    '{"interval_days": 28}'::jsonb               -- monthly cadence (~28 days between runs)
)
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), '
    'sber_index_pull (#887, monthly СберИндекс city-level price index).';

COMMIT;
