-- 096_scrape_schedules_seed_rosreestr_quarter_poll.sql
-- Issue #888 — seed scrape_schedules row for monthly Rosreestr new-quarter poll.
--
-- Context:
--   app/services/rosreestr_poll.py checks the Rosreestr open-data portal for the
--   next unpublished quarter via an HTTP HEAD request (no download). When the dataset
--   ZIP becomes available, a clear actionable INFO log is emitted for the operator.
--
--   Dataset filename pattern:
--     dataset_СДЕЛКИ_r-r_01-92_y_{YYYY}_q_{N}.csv.zip
--   Published at: https://rosreestr.gov.ru/opendata/
--
--   Ingest (operator-run after detection, NOT automated):
--     data/sql/02_load_all_quarters.sh
--     tradein-mvp/deploy/import-rosreestr.sh
--
--   Components deployed together:
--     1. services/rosreestr_poll.py — latest_loaded_quarter() + poll_rosreestr_new_quarter()
--     2. tasks/rosreestr_quarter_poll.py  — run_rosreestr_quarter_poll() task wrapper
--     3. scheduler.py — trigger_rosreestr_quarter_poll_run() + dispatch branch
--        `elif source == "rosreestr_quarter_poll"`
--     4. This migration — seeds the scrape_schedules row (enabled=true, monthly window)
--
-- Schedule window 07:00-08:00 UTC:
--   Monthly cadence, placed after sber_index_pull (05:00-06:00 UTC) and
--   rosreestr_dkp_import (04:00-06:00 UTC) — morning batch slot.
--   The Rosreestr open-data portal publishes new quarters roughly once per quarter
--   (Q2 data lands ~August). Monthly polling is sufficient to catch publication within
--   a few weeks of release without wasting resources on daily checks.
--   default_params.interval_days=28 follows the same monthly-cadence pattern as
--   sber_index_pull (093) and avito/yandex monthly seeds.
--
-- next_run_at bootstrapped to tomorrow 07:00 UTC — scheduler will not fire immediately
-- on deploy (same pattern as 078/079/082/088/091/093).
--
-- Idempotent: ON CONFLICT (source) DO NOTHING — safe to re-apply.
--
-- Dependencies:
--   052_scrape_schedules.sql (table + UNIQUE(source))
--   scheduler.py change (trigger_rosreestr_quarter_poll_run must be deployed)
--   services/rosreestr_poll.py + tasks/rosreestr_quarter_poll.py must be deployed
--
-- Deploy order:
--   Apply after deploying the scheduler.py + task + service changes so the scheduler
--   can dispatch 'rosreestr_quarter_poll' correctly on first fire.

BEGIN;

INSERT INTO scrape_schedules (
    source,
    enabled,
    window_start_hour,
    window_end_hour,
    next_run_at,
    default_params
)
VALUES
(
    'rosreestr_quarter_poll',
    true,                                        -- SAFE: pure HTTP HEAD + DB read, no anti-bot
    7,                                           -- window 07:00-08:00 UTC
    8,
    ((CURRENT_DATE + INTERVAL '1 day') + make_interval(hours => 7)) 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), '
    'rosreestr_quarter_poll (#888, monthly Rosreestr new-quarter availability check).';

COMMIT;
