-- DOM.RF lazy-load pages (housing, housing_dev, realization, mortgage_rates,
-- stat_series) — schema for the 5 pages that didn't capture on the first
-- scrape pass. Realization = 4 endpoints (rpp/total, /housing, /readyYear,
-- /developer) all share the same row shape, so a single wide table works.

-- ── REALIZATION ────────────────────────────────────────────────────────────
-- Per-region (and RF-level when region_code IS NULL) realization data from
-- /аналитика/api/rpp/{total,housing,readyYear,developer}. The "subject"
-- field is overloaded:
--   - total    : RF code (always "100")
--   - housing  : sold-progress bucket id ("0".."6")
--   - ready_year: year of planned readiness ("2026"..)
--   - developer: developer_id (e.g. "6208_0" = PRINZIP)
-- subject_desc carries the human-readable label.
CREATE TABLE IF NOT EXISTS domrf_realization (
    snapshot_date    DATE NOT NULL,
    endpoint_type    TEXT NOT NULL,           -- 'total' | 'housing' | 'ready_year' | 'developer'
    region_code      INT  NOT NULL DEFAULT -1, -- Rosstat code; -1 sentinel for RF-only call
    rep_year         INT  NOT NULL,
    rep_month        INT  NOT NULL,
    type_square      TEXT NOT NULL,           -- 'total' | 'living'
    subject          TEXT NOT NULL,           -- bucket id / year / developer_id
    subject_desc     TEXT,
    share_total      NUMERIC,                 -- доля от итогового объёма (для housing/ready_year)
    total_square     NUMERIC,                 -- м² × тыс
    open_square      NUMERIC,
    sold_square      NUMERIC,
    unsold_square    NUMERIC,
    unopened_square  NUMERIC,
    sold_perc        NUMERIC,
    unsold_perc      NUMERIC,
    unopened_perc    NUMERIC,
    sold_amount      NUMERIC,                 -- # квартир
    price_avg        NUMERIC,                 -- ₽/м² средний
    PRIMARY KEY (snapshot_date, endpoint_type, region_code, type_square, subject)
);
CREATE INDEX IF NOT EXISTS idx_realization_region ON domrf_realization (region_code, endpoint_type);
CREATE INDEX IF NOT EXISTS idx_realization_dev    ON domrf_realization (subject) WHERE endpoint_type='developer';

-- ── STAT_SERIES XLSX FILE INDEX ────────────────────────────────────────────
-- Catalogue of the public XLSX statistics files served at
-- /site/binaries/content/assets/domrf/xlsdashboard/*. Stores download metadata
-- and a copy of the bytes (for offline reload). XLSX bodies live in BYTEA so
-- the prod DB is the single source of truth — no filesystem dependency.
CREATE TABLE IF NOT EXISTS domrf_xlsx_files (
    snapshot_date    DATE NOT NULL,
    filename         TEXT NOT NULL,
    source_url       TEXT NOT NULL,
    bytes            INT  NOT NULL,
    sha256           TEXT NOT NULL,
    content          BYTEA NOT NULL,
    fetched_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (snapshot_date, filename)
);

-- ── SSR BLOBS for housing / housing_dev / mortgage_rates ────────────────────
-- These pages bake their data into Next.js __NEXT_DATA__ instead of XHR.
-- We dump the whole blob into the existing universal store so future
-- normalization can mine it without re-scraping.
-- (no new table — uses existing domrf_raw_endpoints from 31_schema_domrf_raw.sql)
