-- Import unique tables from Anton's domrf.db (SQLite, 23.5MB).
-- Skipped duplicates: regions, federal_districts, mortgage_rates (empty),
-- escrow_banks, rc_classes/dev_size/city_pop, rc_dynamics_rf, egrn_deals
-- (we have raw rosreestr_deals — 4.97M rows, 32× larger).
-- developers (8 618) handled separately via merge into domrf_developers.

-- ── DOM.РФ /сервисы/api/kn/* — поквартирный прайс + ЖК ─────────────────────
CREATE TABLE IF NOT EXISTS domrf_kn_flats (
    id              BIGINT PRIMARY KEY,
    ods_id          TEXT,
    flat_type       TEXT,
    flat_number     TEXT,
    is_studio       BOOLEAN,
    total_area      NUMERIC,
    living_area     NUMERIC,
    rooms           INT,
    status          TEXT,                -- free | booked | sold
    price_rub       BIGINT,
    price_per_m2    NUMERIC,
    floor           INT,
    num_floors      INT,
    obj_id          BIGINT,              -- FK target → domrf_kn_objects.obj_id
    city            TEXT,
    region_cd       INT,
    obj_name        TEXT,
    snapshot_date   DATE NOT NULL DEFAULT CURRENT_DATE
);
CREATE INDEX IF NOT EXISTS idx_kn_flats_obj    ON domrf_kn_flats (obj_id);
CREATE INDEX IF NOT EXISTS idx_kn_flats_status ON domrf_kn_flats (status);
CREATE INDEX IF NOT EXISTS idx_kn_flats_region ON domrf_kn_flats (region_cd);
CREATE INDEX IF NOT EXISTS idx_kn_flats_rooms  ON domrf_kn_flats (rooms);

CREATE TABLE IF NOT EXISTS domrf_kn_objects (
    obj_id          BIGINT PRIMARY KEY,
    hobj_id         BIGINT,
    comm_name       TEXT,
    addr            TEXT,
    short_addr      TEXT,
    region_cd       INT,
    dev_id          TEXT,
    dev_name        TEXT,
    dev_inn         TEXT,
    floor_min       INT,
    floor_max       INT,
    flat_count      INT,
    square_living   NUMERIC,
    ready_dt        DATE,
    problem_flag    TEXT,
    site_status     TEXT,
    green_house     BOOLEAN,
    escrow          BOOLEAN,
    obj_class       TEXT,
    wall_type       TEXT,
    energy_eff      TEXT,
    latitude        NUMERIC,
    longitude       NUMERIC,
    obj_status      INT,
    is_prinzip      BOOLEAN DEFAULT FALSE,
    is_ekb          BOOLEAN DEFAULT FALSE,
    is_problem      BOOLEAN DEFAULT FALSE,
    snapshot_date   DATE NOT NULL DEFAULT CURRENT_DATE
);
CREATE INDEX IF NOT EXISTS idx_kn_obj_dev      ON domrf_kn_objects (dev_id);
CREATE INDEX IF NOT EXISTS idx_kn_obj_region   ON domrf_kn_objects (region_cd);
CREATE INDEX IF NOT EXISTS idx_kn_obj_prinzip  ON domrf_kn_objects (is_prinzip);
CREATE INDEX IF NOT EXISTS idx_kn_obj_ekb      ON domrf_kn_objects (is_ekb);

-- ── ЦБ РФ ипотечная статистика (cbr.ru/statistics/bank_sector/mortgage/) ──
CREATE TABLE IF NOT EXISTS cbr_mortgage_files (
    filename     TEXT PRIMARY KEY,
    size_kb      INT,
    description  TEXT,
    url          TEXT
);

CREATE TABLE IF NOT EXISTS cbr_mortgage_series (
    series_id    TEXT NOT NULL,            -- e.g. '02_05_Debt_ind_в_рублях'
    region       TEXT NOT NULL,            -- 'rf' | 'sverdl'
    period       TEXT NOT NULL,            -- 'Feb 2019', 'Mar 2026', ...
    value        NUMERIC,
    title        TEXT,
    PRIMARY KEY (series_id, region, period)
);
CREATE INDEX IF NOT EXISTS idx_cbr_period ON cbr_mortgage_series (period);
CREATE INDEX IF NOT EXISTS idx_cbr_region ON cbr_mortgage_series (region);

-- ── Сбер ДомКлик DDF (PDF nov2025) — ключевые метрики ────────────────────
CREATE TABLE IF NOT EXISTS sber_ddf_metrics (
    metric_key   TEXT PRIMARY KEY,
    region       TEXT,
    value_2019   NUMERIC,
    value_2025   NUMERIC,
    unit         TEXT,
    source       TEXT
);

-- ── Yandex Realty ЕКБ ─────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS yandex_realty_zk (
    yid             BIGINT PRIMARY KEY,
    name            TEXT,
    developer       TEXT,
    developer_id    BIGINT,
    finished_obj    INT,
    unfinished_obj  INT,
    price_from      NUMERIC,
    price_to        NUMERIC,
    address         TEXT,
    obj_class       TEXT,
    latitude        NUMERIC,
    longitude       NUMERIC,
    snapshot_date   DATE NOT NULL DEFAULT CURRENT_DATE
);

CREATE TABLE IF NOT EXISTS yandex_realty_class_prices (
    obj_class           TEXT PRIMARY KEY,
    total_sites         INT,
    price_per_m2_min    NUMERIC,
    price_per_m2_max    NUMERIC,
    snapshot_date       DATE NOT NULL DEFAULT CURRENT_DATE
);

-- ── Geo Свердл/ЕКБ ─────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS ekb_districts (
    district_name        TEXT PRIMARY KEY,
    population           INT,
    zk_count             INT,
    flat_count           INT,
    area_m2              NUMERIC,
    median_price_per_m2  NUMERIC,
    mean_price_per_m2    NUMERIC
);

CREATE TABLE IF NOT EXISTS ekb_metro (
    station_name         TEXT PRIMARY KEY,
    zk_count             INT,
    median_price_per_m2  NUMERIC
);

CREATE TABLE IF NOT EXISTS sv_cities (
    city_name            TEXT PRIMARY KEY,
    zk_count             INT,
    flat_count           INT,
    area_m2              NUMERIC,
    median_price_per_m2  NUMERIC
);

-- ── Pre-aggregated Свердл developer rankings (топ-50, март 2026) ──────────
CREATE TABLE IF NOT EXISTS sv_dev_sales (
    rank              INT,
    dev_id            TEXT NOT NULL,
    name              TEXT,
    total_square      INT,
    sold_perc         NUMERIC,
    unsold_perc       NUMERIC,
    unopened_perc     NUMERIC,
    rep_period        TEXT NOT NULL,         -- 'YYYY-MM' (e.g. '2026-03')
    PRIMARY KEY (dev_id, rep_period)
);

-- ── Developer growth 2024→2025 (РФ + Свердл) ───────────────────────────────
CREATE TABLE IF NOT EXISTS domrf_dev_growth (
    id                BIGSERIAL PRIMARY KEY,
    dev_id            TEXT,
    dev_name          TEXT NOT NULL,
    area_2024         NUMERIC,
    area_2025         NUMERIC,
    delta_pct         NUMERIC,
    region            TEXT,                 -- 'rf' | 'sverdl'
    UNIQUE (dev_name, region)
);

-- ── Росреестр rent deals Свердл (1071 договоров аренды) ──────────────────
CREATE TABLE IF NOT EXISTS rosreestr_rent_deals (
    id                BIGSERIAL PRIMARY KEY,
    quarter           TEXT NOT NULL,        -- 'Q1 2026'
    obj_type          TEXT,                 -- '002001001000' и т.п.
    cad_quarter       TEXT,                 -- '66:02:1401003'
    area              NUMERIC,
    rent_rub          NUMERIC,
    rent_per_m2       NUMERIC,
    duration_months   INT,
    region_cd         INT DEFAULT 66
);
CREATE INDEX IF NOT EXISTS idx_rent_quarter   ON rosreestr_rent_deals (quarter);
CREATE INDEX IF NOT EXISTS idx_rent_cadq      ON rosreestr_rent_deals (cad_quarter);
CREATE INDEX IF NOT EXISTS idx_rent_region    ON rosreestr_rent_deals (region_cd);

COMMENT ON TABLE domrf_kn_flats IS 'DOM.РФ /сервисы/api/kn/flats per-flat catalog. 27 563 квартир РФ status=free/booked/sold с ценами. Источник: Anton domrf.db imported 27.04.2026.';
COMMENT ON TABLE domrf_kn_objects IS 'DOM.РФ /сервисы/api/kn/object — реестр ЖК Свердл. 1 516 объектов с GPS, классом, эскроу-флагом. is_prinzip flag для 28 PRINZIP ЖК. Источник: Anton.';
COMMENT ON TABLE cbr_mortgage_series IS 'ЦБ РФ статистика по ипотеке (XLSX cbr.ru/statistics/bank_sector/mortgage/). 47 серий × {rf|sverdl} × месяцы 2019-2026. Свердл Фев-2026: ставка 11%, +318 б.п. за месяц.';
COMMENT ON TABLE sber_ddf_metrics IS 'Сбер ДомКлик DDF nov2025 PDF: affordability, demand index, миграция, повторные ипотечники, price gap 79/21%, quality shift.';
COMMENT ON TABLE yandex_realty_zk IS 'Yandex Realty snapshot — 12 ЖК ЕКБ из INITIAL_STATE с классами/ценами. Полный snapshot пользовательских listings.';
COMMENT ON TABLE rosreestr_rent_deals IS 'Договоры аренды Свердл Q1 2026 (1071) из Росреестра data-sets.';
