-- 019_listings_alter_cian.sql
-- Purpose: Add Cian-specific columns to listings table (SERP + detail fields).
-- Dependencies: listings table created in 002_core_tables.sql / 011_listings_alter.sql
-- Deploy order: Apply after 018_avito_imv_evaluations.sql
--
-- Fields sourced from:
--   - Schema_Cian_SERP_Inventory sec 16.1 (SERP-level Cian fields)
--   - Schema_Cian_SERP_Inventory sec 20.3 (detail page + stats)
--   - CianScraper_v1_Implementation_Plan Stage 1

BEGIN;

ALTER TABLE listings
  -- Cian detail page: window/bathroom/ceiling data
  ADD COLUMN IF NOT EXISTS windows_view_type      text,         -- 'yardAndStreet'/'yard'/'street'/'park'
  ADD COLUMN IF NOT EXISTS separate_wcs_count     int,          -- раздельных санузлов
  ADD COLUMN IF NOT EXISTS combined_wcs_count     int,          -- совмещённых санузлов
  ADD COLUMN IF NOT EXISTS ceiling_height         numeric(3,2), -- из building.ceilingHeight, напр. 2.5

  -- Cian detail: repair / finish type (вторичка)
  ADD COLUMN IF NOT EXISTS repair_type            text,         -- without/cosmetic/euro/designer

  -- Cian detail: stats (из offerData.stats)
  ADD COLUMN IF NOT EXISTS views_total            int,          -- суммарные просмотры
  ADD COLUMN IF NOT EXISTS views_today            int,          -- просмотры за сегодня

  -- Cian SERP: living area + bedroom/balcony breakdown
  ADD COLUMN IF NOT EXISTS living_area_m2         numeric(7,2), -- жилая площадь (только Cian)
  ADD COLUMN IF NOT EXISTS bedrooms_count         int,          -- кол-во спален
  ADD COLUMN IF NOT EXISTS balconies_count        int,          -- балконов
  ADD COLUMN IF NOT EXISTS loggias_count          int,          -- лоджий

  -- Cian SERP: dedup/matching helpers
  ADD COLUMN IF NOT EXISTS description_minhash    text,         -- Cian LSH minhash for dedup
  ADD COLUMN IF NOT EXISTS cadastral_number       text,         -- кадастровый номер квартиры

  -- Cian SERP: building cadastral (for house matching)
  ADD COLUMN IF NOT EXISTS building_cadastral_number text,      -- кадастровый номер здания

  -- Cian SERP: seller info (structured, unlike Avito text)
  ADD COLUMN IF NOT EXISTS phones                 jsonb,        -- [{countryCode, number, type}, ...]
  ADD COLUMN IF NOT EXISTS is_homeowner           boolean,      -- продаёт собственник (isOwner)
  ADD COLUMN IF NOT EXISTS is_pro_seller          boolean,      -- PRO подписка у продавца

  -- Cian SERP: bargain/sale type flags
  ADD COLUMN IF NOT EXISTS bargain_allowed        boolean,      -- торг возможен
  ADD COLUMN IF NOT EXISTS sale_type              text,         -- 'free'/'alternative'/'new'

  -- Cian SERP: newbuilding house link
  ADD COLUMN IF NOT EXISTS house_source           text,         -- 'cian' (источник house record)
  ADD COLUMN IF NOT EXISTS house_ext_id           text,         -- cian newbuilding id (строка)

  -- Cian SERP: metro stations near offer
  ADD COLUMN IF NOT EXISTS metro_stations         jsonb;        -- [{name, distanceKm, travelTime, travelType}, ...]

-- Partial index for fast minhash dedup lookups
CREATE INDEX IF NOT EXISTS listings_minhash_idx
  ON listings (description_minhash)
  WHERE description_minhash IS NOT NULL;

-- Partial index on cadastral_number for Tier-0 apartment matching
CREATE INDEX IF NOT EXISTS listings_cadastral_idx
  ON listings (cadastral_number)
  WHERE cadastral_number IS NOT NULL;

-- Partial index on building_cadastral_number for house matching
CREATE INDEX IF NOT EXISTS listings_bld_cadastral_idx
  ON listings (building_cadastral_number)
  WHERE building_cadastral_number IS NOT NULL;

-- GIN on metro_stations for JSON querying
CREATE INDEX IF NOT EXISTS listings_metro_stations_gin_idx
  ON listings USING GIN (metro_stations)
  WHERE metro_stations IS NOT NULL;

COMMIT;
