-- 045_house_placement_history_extend.sql
-- Purpose: Add source_confidence + notes + per-house 3-col UNIQUE per Cross_Source sec 14.1.
--          Table base in 017; source/ext_item_id already present.
--          Note: plan calls the external-id column "ext_listing_id", but existing
--          schema uses "ext_item_id" — semantic alias documented in COMMENT.
-- Dependencies: 017_house_placement_history.sql, 032_yandex_history.sql

BEGIN;

ALTER TABLE house_placement_history
  ADD COLUMN IF NOT EXISTS source_confidence real DEFAULT 1.0,
  ADD COLUMN IF NOT EXISTS notes             text;

CREATE UNIQUE INDEX IF NOT EXISTS hph_house_source_extid_uniq_idx
  ON house_placement_history (house_id, source, ext_item_id)
  WHERE house_id IS NOT NULL;

COMMENT ON COLUMN house_placement_history.source_confidence IS
  'Confidence (0-1) that this history row belongs to the linked house. '
  '1.0 for direct Avito Houses Catalog / Cian offer; lower for Yandex Valuation synthetic IDs.';
COMMENT ON COLUMN house_placement_history.notes IS
  'Free-form notes — used by dedup logic (e.g. ''dup_of:cian_offer:123''). '
  'See Cross_Source_Matching_Strategy sec 14.3.';
COMMENT ON COLUMN house_placement_history.ext_item_id IS
  'External listing id (alias of "ext_listing_id" in Master Plan sec 14.1).';

COMMIT;
