-- 024_houses_price_dynamics.sql
-- Purpose: Monthly price time-series per house (from Cian newbuilding realtyValuation.priceDynamics).
--          Stores per room_count / prices_type / period granularity as provided by Cian.
-- Dependencies:
--   - houses table (009_houses.sql)
-- Deploy order: Apply after 023.
--
-- Sources: Schema_Cian_SERP_Inventory sec 16.3

BEGIN;

CREATE TABLE IF NOT EXISTS houses_price_dynamics (
    id              bigserial   PRIMARY KEY,
    house_id        bigint      NOT NULL REFERENCES houses(id) ON DELETE CASCADE,
    month_date      date        NOT NULL,             -- '2025-11-01' (first of month)
    price_per_sqm   numeric     NOT NULL,             -- avg price per sqm for that month
    source          text        NOT NULL DEFAULT 'cian_realty_valuation',
    recorded_at     timestamptz NOT NULL DEFAULT NOW(),
    UNIQUE (house_id, month_date, source)
);

-- Primary read pattern: time-ordered series for a house
CREATE INDEX IF NOT EXISTS hpd_house_date_idx
  ON houses_price_dynamics (house_id, month_date DESC);

COMMIT;
