-- DOM.RF (наш.дом.рф) portal-analytics dashboard data.
-- Source: https://xn--80az8a.xn--d1aqf.xn--p1ai/portal-analytics/api/*
-- Coverage: aggregate of construction-stage flats in МКД (multi-apartment buildings).
-- ⚠ NOT historical: this is a SNAPSHOT of "currently being built" state. Updated continuously.
-- We store snapshot_date so we can rebuild trend over time by re-pulling.

-- 1. Federal districts + regions dictionary (from /dictionaries/regions)
CREATE TABLE IF NOT EXISTS domrf_regions (
    region_id           INT PRIMARY KEY,
    region_name         TEXT NOT NULL,
    federal_district    TEXT NOT NULL
);

-- 2. Developers dictionary (from /dictionaries/developers, 2734 entries as of 2026-04-26)
CREATE TABLE IF NOT EXISTS domrf_developers (
    developer_id        TEXT PRIMARY KEY,             -- '6208_0' (PRINZIP), '5655_0' (ПИК), '0_14583' etc.
    developer_name      TEXT NOT NULL
);

-- 3. Snapshot metadata
CREATE TABLE IF NOT EXISTS domrf_snapshots (
    snapshot_date       DATE PRIMARY KEY,
    fetched_at          TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    notes               TEXT
);

-- 4. By-region aggregates with breakdown by room count
-- One row per (region, room_count_type, snapshot)
CREATE TABLE IF NOT EXISTS domrf_region_aggregates (
    region_id           INT NOT NULL,
    snapshot_date       DATE NOT NULL,
    room_count_type     TEXT NOT NULL,                -- 'ONE'|'TWO'|'THREE'|'FOUR' or 'TOTAL' for region totals
    flat_count          INT NOT NULL,
    area_sqm            NUMERIC(14, 2) NOT NULL,
    percent             SMALLINT,                     -- % within region for room_count breakdown
    PRIMARY KEY (region_id, snapshot_date, room_count_type)
);

-- 5. By-flat-area distribution (RF-wide, не работает фильтр по региону)
CREATE TABLE IF NOT EXISTS domrf_flat_area_distribution (
    snapshot_date       DATE NOT NULL,
    region_id           INT NOT NULL DEFAULT 0,       -- 0 = вся РФ (api ignores ?regionId on this page)
    area_bucket         TEXT NOT NULL,                -- 'FROM_0_TO_25', 'FROM_25_TO_35', ..., 'FROM_100'
    room_count_type     TEXT NOT NULL,                -- 'ONE'|'TWO'|'THREE'|'FOUR' or 'TOTAL'
    flat_count          INT NOT NULL,
    area_sqm            NUMERIC(14, 2) NOT NULL,
    percent             SMALLINT,
    PRIMARY KEY (snapshot_date, region_id, area_bucket, room_count_type)
);

-- 6. By-developer aggregates with breakdown by room count
-- Topology: developer × snapshot × room_count_type
CREATE TABLE IF NOT EXISTS domrf_developer_aggregates (
    developer_id        TEXT NOT NULL,
    snapshot_date       DATE NOT NULL,
    region_id           INT NOT NULL DEFAULT 0,       -- 0 = РФ агрегат (api ignores ?regionId on this page)
    room_count_type     TEXT NOT NULL,
    flat_count          INT NOT NULL,
    area_sqm            NUMERIC(14, 2) NOT NULL,
    percent             SMALLINT,
    PRIMARY KEY (developer_id, snapshot_date, region_id, room_count_type)
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_domrf_region_aggregates_room    ON domrf_region_aggregates (room_count_type);
CREATE INDEX IF NOT EXISTS idx_domrf_developer_aggregates_room ON domrf_developer_aggregates (room_count_type);
CREATE INDEX IF NOT EXISTS idx_domrf_developer_aggregates_dev  ON domrf_developer_aggregates (developer_id);
