-- 020_houses_alter_cian.sql
-- Purpose: Add Cian-specific and BTI columns to houses table.
-- Dependencies:
--   - houses table: 009_houses.sql
--   - management_companies table: 021_management_companies.sql
--     NOTE: management_company_id FK added here but the REFERENCES target (management_companies)
--     must exist first. Apply 021 BEFORE adding the FK column, OR apply 020 without the FK
--     and add FK separately. To keep idempotent ordering: management_companies is created in 021,
--     so 021 must be applied before 020 in production. Alternatively, add FK in 021 via ALTER.
--     DECISION: management_company_id column added without FK here; FK constraint added in 021
--     after management_companies table is created. This file only adds plain bigint column.
-- Deploy order: Apply after 019. Apply 021 after 020 to complete FK wiring.
--
-- Sources: Schema_Cian_SERP_Inventory sec 16.5, 20.3, 26.3

BEGIN;

ALTER TABLE houses
  -- BTI data (from offerData.bti.houseData — вторичка detail page)
  ADD COLUMN IF NOT EXISTS series_name            text,              -- 'I-447' / 'II-49' / etc (Cian BTI)
  ADD COLUMN IF NOT EXISTS entrances              int,               -- подъездов в доме
  ADD COLUMN IF NOT EXISTS flat_count             int,               -- квартир в доме (liquidity proxy)
  ADD COLUMN IF NOT EXISTS is_emergency           boolean,           -- аварийный дом (red flag!)
  ADD COLUMN IF NOT EXISTS heat_supply_type       text,              -- central/individual/none
  ADD COLUMN IF NOT EXISTS gas_supply_type        text,              -- central/none
  ADD COLUMN IF NOT EXISTS overlap_type           text,              -- concrete/wood/steel

  -- Cian newbuilding / valuation specific
  ADD COLUMN IF NOT EXISTS cian_internal_house_id bigint,            -- Cian's houseId (filters.houseId)
  ADD COLUMN IF NOT EXISTS management_company_id  bigint,            -- FK to management_companies(id) added in 021
  ADD COLUMN IF NOT EXISTS transport_accessibility_rate int,         -- 0-10 transport score

  -- Cian newbuilding jsonb arrays
  ADD COLUMN IF NOT EXISTS advantages             jsonb,             -- typed advantage list
  ADD COLUMN IF NOT EXISTS banks                  jsonb,             -- банки-партнёры (ipoteka)
  ADD COLUMN IF NOT EXISTS builders               jsonb,             -- застройщики (может быть несколько)
  ADD COLUMN IF NOT EXISTS houses_by_turn         jsonb;             -- группировка корпусов по очередям

-- Partial index on series_name for BTI series analytics
CREATE INDEX IF NOT EXISTS houses_series_idx
  ON houses (series_name)
  WHERE series_name IS NOT NULL;

-- Partial index for emergency flag (rare, useful for risk filter)
CREATE INDEX IF NOT EXISTS houses_emergency_idx
  ON houses (is_emergency)
  WHERE is_emergency = true;

-- Partial index for Cian internal house ID (cross-source matching)
CREATE INDEX IF NOT EXISTS houses_cian_iid_idx
  ON houses (cian_internal_house_id)
  WHERE cian_internal_house_id IS NOT NULL;

-- GIN on advantages for feature-based filtering
CREATE INDEX IF NOT EXISTS houses_advantages_gin_idx
  ON houses USING GIN (advantages)
  WHERE advantages IS NOT NULL;

-- GIN on banks
CREATE INDEX IF NOT EXISTS houses_banks_gin_idx
  ON houses USING GIN (banks)
  WHERE banks IS NOT NULL;

-- GIN on builders
CREATE INDEX IF NOT EXISTS houses_builders_gin_idx
  ON houses USING GIN (builders)
  WHERE builders IS NOT NULL;

COMMIT;
