-- DOM.RF analytics, normalized schema for scraper output (data/raw/domrf_full/*).
-- Snapshot date stamped per scrape; idempotent UPSERT loaders.

-- ── 1. Launch (запуски + ввод в эксплуатацию) ────────────────────────────────
-- Topology: page returns top-N entities (developers/regions/areas/fo) with launch (app)
-- and commissioning (rnv) values. Single table covers all 4 dimensions via dim_type.
CREATE TABLE IF NOT EXISTS domrf_launch_top (
    snapshot_date    DATE   NOT NULL,
    rep_year         INT    NOT NULL,
    calc_type        TEXT   NOT NULL,         -- 'SQUARE' (м²) | 'HOUSES' (шт) | 'FLATS' (шт)
    dim_type         TEXT   NOT NULL,         -- 'developer' | 'region' | 'area' | 'fo'
    metric_type      TEXT   NOT NULL,         -- 'app' (запуски) | 'rnv' (ввод)
    entity_id        TEXT   NOT NULL,         -- '6072_0' (developer) | '50' (region) | 'Москва' (area)
    entity_name      TEXT   NOT NULL,
    value            NUMERIC NOT NULL,
    PRIMARY KEY (snapshot_date, rep_year, calc_type, dim_type, metric_type, entity_id)
);
CREATE INDEX IF NOT EXISTS idx_launch_top_dim ON domrf_launch_top (dim_type, metric_type, value DESC);

-- Объёмы по классам жилья
CREATE TABLE IF NOT EXISTS domrf_launch_obj_class (
    snapshot_date    DATE   NOT NULL,
    rep_year         INT    NOT NULL,
    calc_type        TEXT   NOT NULL,
    obj_class_cd     INT    NOT NULL,         -- 1=Типовой, 2=Комфорт, 3=Бизнес, 4=Элитный
    obj_class_desc   TEXT   NOT NULL,
    app_value        NUMERIC,
    rnv_value        NUMERIC,
    PRIMARY KEY (snapshot_date, rep_year, calc_type, obj_class_cd)
);

-- Time series: год × месяц → запуски/вводы
CREATE TABLE IF NOT EXISTS domrf_launch_monthly (
    snapshot_date    DATE   NOT NULL,
    calc_type        TEXT   NOT NULL,
    rep_year         INT    NOT NULL,
    rep_month        INT    NOT NULL,         -- 1..12
    app_value        NUMERIC,
    rnv_value        NUMERIC,
    PRIMARY KEY (snapshot_date, calc_type, rep_year, rep_month)
);

-- ── 2. Реализация / распроданность-стройготовность (sold_ready) ──────────────
CREATE TABLE IF NOT EXISTS domrf_sold_ready_index (
    snapshot_date         DATE NOT NULL,
    rep_year              INT  NOT NULL,
    rep_month             INT  NOT NULL,
    square_sum            NUMERIC,            -- всего стр. площади РФ, м²
    sold_perc             NUMERIC,            -- % проданного
    sold_sum              NUMERIC,            -- проданная площадь, м²
    sold_ready_perc       NUMERIC,            -- "распроданность ÷ готовность"
    ready_perc            NUMERIC,            -- % готовности
    PRIMARY KEY (snapshot_date, rep_year, rep_month)
);

-- Reality breakdowns: foChart / regionChart / cityChart / devChart …
CREATE TABLE IF NOT EXISTS domrf_sold_ready_breakdown (
    snapshot_date    DATE   NOT NULL,
    rep_year         INT    NOT NULL,
    rep_month        INT    NOT NULL,
    chart_type       TEXT   NOT NULL,         -- 'foChart' | 'regionChart' | 'cityChart' | 'devChart' | ...
    entity_key       TEXT   NOT NULL,         -- название/код
    square_sum       NUMERIC,
    sold_perc        NUMERIC,
    ready_perc       NUMERIC,
    sold_ready_perc  NUMERIC,
    fo_cd            INT,
    extra            JSONB,                   -- backup для прочих полей
    PRIMARY KEY (snapshot_date, rep_year, rep_month, chart_type, entity_key)
);

-- Динамика: chartType × год × месяц → значение
CREATE TABLE IF NOT EXISTS domrf_sold_ready_dynamics (
    snapshot_date         DATE NOT NULL,
    dynamic_chart_type    TEXT NOT NULL,      -- 'squareSumChart' | 'soldPercChart' | 'readyPercChart' | 'soldReadyPercChart'
    rep_year              INT  NOT NULL,
    rep_month             INT  NOT NULL,
    value                 NUMERIC,
    PRIMARY KEY (snapshot_date, dynamic_chart_type, rep_year, rep_month)
);

-- ── 3. Share construction (долевое стр-во / project finance) ────────────────
-- /аналитика/api/project/finance/dashboard returns rows per region/FO/RF.
CREATE TABLE IF NOT EXISTS domrf_project_finance (
    snapshot_date              DATE   NOT NULL,
    report_date                DATE,
    subject_type               TEXT,         -- 'rf' | 'fo' | 'region'
    subject_name               TEXT,
    region_cd                  INT,
    fo_cd                      INT,
    fo_desc                    TEXT,
    rns_cnt                    INT,          -- разрешения на стр-во
    dev_cnt                    INT,          -- застройщики
    liv_sq_amt                 NUMERIC,      -- жилая площадь м²
    guaranty_escrow_rns_cnt    INT,          -- эскроу разрешения
    guaranty_escrow_dev_cnt    INT,
    guaranty_escrow_liv_sq_amt NUMERIC,
    guaranty_zosg_rns_cnt      INT,
    guaranty_zosg_dev_cnt      INT,
    guaranty_zosg_liv_sq_amt   NUMERIC,
    guaranty_rns_cnt           INT,          -- всего по 214-ФЗ
    guaranty_dev_cnt           INT,
    guaranty_liv_sq_amt        NUMERIC,
    nonguaranty_rns_cnt        INT,          -- ПП №480
    nonguaranty_dev_cnt        INT,
    nonguaranty_liv_sq_amt     NUMERIC,
    extra                      JSONB,
    id                         BIGSERIAL PRIMARY KEY
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_project_finance_snap
    ON domrf_project_finance (snapshot_date, COALESCE(subject_type, ''), COALESCE(subject_name, ''));

-- ── 4. Ввод жилья (commissioning building_summary) ──────────────────────────
CREATE TABLE IF NOT EXISTS domrf_commissioning (
    snapshot_date         DATE NOT NULL,
    region_id             INT  NOT NULL,
    region_name           TEXT NOT NULL,
    region_type           TEXT,            -- 'rf' | 'fo' | 'region'
    reporting_period      DATE NOT NULL,
    rep_year              INT,
    accumulated_fact_area              NUMERIC,
    accumulated_fact_area_multifamily  NUMERIC,
    accumulated_fact_area_private      NUMERIC,
    accumulated_fact_area_change       NUMERIC,
    accumulated_fact_area_change_share NUMERIC,
    extra                 JSONB,
    PRIMARY KEY (snapshot_date, region_id, reporting_period)
);

-- ── 5. Mortgage stats ───────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS domrf_mortgage_dashboard (
    snapshot_date                  DATE NOT NULL PRIMARY KEY,
    total_credit_count             INT,
    total_credit_count_delta_pct   NUMERIC,
    primary_credit_count           INT,
    primary_credit_count_delta_pct NUMERIC,
    secondary_credit_count         INT,
    secondary_credit_count_delta_pct NUMERIC,
    total_credit_amount            NUMERIC,
    total_credit_amount_delta_pct  NUMERIC,
    primary_credit_amount          NUMERIC,
    primary_credit_amount_delta_pct  NUMERIC,
    secondary_credit_amount        NUMERIC,
    secondary_credit_amount_delta_pct  NUMERIC,
    total_credit_avg_rate          NUMERIC,
    total_credit_avg_rate_delta    NUMERIC,
    primary_credit_avg_rate        NUMERIC,
    primary_credit_avg_rate_delta  NUMERIC,
    secondary_credit_avg_rate      NUMERIC,
    secondary_credit_avg_rate_delta NUMERIC,
    extra                          JSONB
);

CREATE TABLE IF NOT EXISTS domrf_mortgage_details (
    snapshot_date              DATE NOT NULL,
    currency                   TEXT NOT NULL,
    credit_amount_avg          NUMERIC,
    credit_amount_avg_delta_pct NUMERIC,
    credit_avg_period          NUMERIC,
    credit_avg_period_delta_pct NUMERIC,
    credit_debts_amount        NUMERIC,
    credit_debts_amount_delta_pct NUMERIC,
    credit_debts_overdue_percent NUMERIC,
    credit_debts_overdue_percent_delta NUMERIC,
    PRIMARY KEY (snapshot_date, currency)
);
