-- 040_houses_extend.sql
-- Purpose: Add complex_id FK placeholder + per-source enrichment timestamps to houses.
--          Most planned columns (address_fingerprint, cadastral_number, commission_year/month,
--          corpus_count, total_area_ha, has_panorama, yandex_jk_id/slug, yandex_validated_at,
--          cian_internal_house_id) already added in earlier migrations (028/029/020/031).
-- Dependencies: 009_houses.sql (houses table)
-- Apply after: 032_yandex_history.sql

BEGIN;

ALTER TABLE houses
  ADD COLUMN IF NOT EXISTS complex_id          bigint,
  ADD COLUMN IF NOT EXISTS avito_validated_at  timestamptz,
  ADD COLUMN IF NOT EXISTS cian_validated_at   timestamptz;

CREATE INDEX IF NOT EXISTS houses_complex_id_idx
  ON houses (complex_id) WHERE complex_id IS NOT NULL;

COMMENT ON COLUMN houses.complex_id IS
  'FK placeholder for future complexes (ЖК-level) table. NULL until ЖК aggregation introduced.';
COMMENT ON COLUMN houses.avito_validated_at IS
  'Timestamp of last enrichment from Avito Houses Catalog (mirror of yandex_validated_at semantics).';
COMMENT ON COLUMN houses.cian_validated_at IS
  'Timestamp of last enrichment from Cian newbuilding/BTI (mirror of yandex_validated_at semantics).';

COMMIT;
