INSERT INTO audit.data_sources (name, source_type, url, cadence, reliability, notes)
VALUES
    ('sample', 'manual', NULL, 'ad hoc', 'demo', 'Demonstration data. Not real market data.')
ON CONFLICT (name) DO UPDATE
SET source_type = EXCLUDED.source_type,
    cadence = EXCLUDED.cadence,
    reliability = EXCLUDED.reliability,
    notes = EXCLUDED.notes;

INSERT INTO silver.areas (area_id, name, district, segment, notes)
VALUES
    ('qiantan', '前滩', '浦东新区', '核心改善', '产业和公共配套强，供应节奏需要持续跟踪'),
    ('xujiahui', '徐家汇', '徐汇区', '核心成熟', '成熟商圈和教育医疗资源强，价格弹性通常较小'),
    ('danning', '大宁', '静安区', '内中环改善', '居住氛围和商业配套较均衡'),
    ('zhangjiang', '张江', '浦东新区', '产业成长', '产业人口支撑强，产品分化明显'),
    ('hongqiao', '大虹桥', '闵行区', '规划成长', '受商务区兑现和供应影响较大')
ON CONFLICT (area_id) DO UPDATE
SET name = EXCLUDED.name,
    district = EXCLUDED.district,
    segment = EXCLUDED.segment,
    notes = EXCLUDED.notes,
    updated_at = now();

INSERT INTO silver.area_monthly_metrics (
    area_id,
    month,
    transaction_count,
    transaction_price_psm,
    listing_count,
    listing_price_psm,
    median_days_on_market,
    rent_price_psm,
    new_supply_units,
    land_residential_gfa_sqm,
    mortgage_rate_pct,
    policy_signal,
    source
)
VALUES
    ('qiantan', '2026-05', 93, 122500, 380, 126500, 53, 192, 60, 0, 3.35, 1, 'sample'),
    ('xujiahui', '2026-05', 67, 127500, 348, 133000, 66, 207, 20, 0, 3.35, 1, 'sample'),
    ('danning', '2026-05', 103, 98200, 488, 102200, 69, 158, 110, 0, 3.35, 1, 'sample'),
    ('zhangjiang', '2026-05', 126, 84600, 570, 89000, 60, 139, 180, 0, 3.35, 1, 'sample'),
    ('hongqiao', '2026-05', 82, 75800, 735, 82000, 96, 119, 360, 0, 3.35, 0, 'sample')
ON CONFLICT (area_id, month) DO UPDATE
SET transaction_count = EXCLUDED.transaction_count,
    transaction_price_psm = EXCLUDED.transaction_price_psm,
    listing_count = EXCLUDED.listing_count,
    listing_price_psm = EXCLUDED.listing_price_psm,
    median_days_on_market = EXCLUDED.median_days_on_market,
    rent_price_psm = EXCLUDED.rent_price_psm,
    new_supply_units = EXCLUDED.new_supply_units,
    land_residential_gfa_sqm = EXCLUDED.land_residential_gfa_sqm,
    mortgage_rate_pct = EXCLUDED.mortgage_rate_pct,
    policy_signal = EXCLUDED.policy_signal,
    source = EXCLUDED.source,
    updated_at = now();

INSERT INTO gold.area_scores (
    area_id,
    name,
    district,
    segment,
    month,
    investment_score,
    recommendation,
    liquidity_score,
    momentum_score,
    rent_support_score,
    safety_margin_score,
    credit_support_score,
    supply_risk_score,
    valuation_pressure_score,
    transaction_count,
    transaction_price_psm,
    listing_count,
    listing_price_psm,
    rent_price_psm,
    median_days_on_market,
    annual_rent_yield_pct,
    listing_pressure_ratio,
    discount_pct,
    price_momentum_pct,
    volume_momentum_pct,
    model_version
)
VALUES
    ('zhangjiang', '张江', '浦东新区', '产业成长', '2026-05', 72.0, '观察池', 93.0, 60.5, 69.4, 41.6, 66.7, 21.8, 62.9, 126, 84600, 570, 89000, 139, 60, 1.97, 4.52, 4.94, 1.32, 6.78, 'sample-v1'),
    ('qiantan', '前滩', '浦东新区', '核心改善', '2026-05', 66.4, '观察池', 79.2, 61.6, 62.9, 25.4, 66.7, 8.1, 78.6, 93, 122500, 380, 126500, 192, 53, 1.88, 4.09, 3.16, 1.24, 8.14, 'sample-v1'),
    ('danning', '大宁', '静安区', '内中环改善', '2026-05', 65.6, '观察池', 76.9, 57.9, 66.5, 32.4, 66.7, 20.0, 72.8, 103, 98200, 488, 102200, 158, 69, 1.93, 4.74, 3.91, 1.24, 5.10, 'sample-v1'),
    ('xujiahui', '徐家汇', '徐汇区', '核心成熟', '2026-05', 61.9, '中性观望', 59.8, 57.1, 67.7, 34.5, 66.7, 18.5, 70.2, 67, 127500, 348, 133000, 207, 66, 1.95, 5.19, 4.14, 0.55, 8.06, 'sample-v1'),
    ('hongqiao', '大虹桥', '闵行区', '规划成长', '2026-05', 55.2, '中性观望', 53.5, 50.9, 63.1, 73.4, 59.2, 99.6, 24.6, 82, 75800, 735, 82000, 119, 96, 1.88, 8.96, 7.56, 0.40, 3.80, 'sample-v1')
ON CONFLICT (area_id, month) DO UPDATE
SET investment_score = EXCLUDED.investment_score,
    recommendation = EXCLUDED.recommendation,
    liquidity_score = EXCLUDED.liquidity_score,
    momentum_score = EXCLUDED.momentum_score,
    rent_support_score = EXCLUDED.rent_support_score,
    safety_margin_score = EXCLUDED.safety_margin_score,
    credit_support_score = EXCLUDED.credit_support_score,
    supply_risk_score = EXCLUDED.supply_risk_score,
    valuation_pressure_score = EXCLUDED.valuation_pressure_score,
    transaction_count = EXCLUDED.transaction_count,
    transaction_price_psm = EXCLUDED.transaction_price_psm,
    listing_count = EXCLUDED.listing_count,
    listing_price_psm = EXCLUDED.listing_price_psm,
    rent_price_psm = EXCLUDED.rent_price_psm,
    median_days_on_market = EXCLUDED.median_days_on_market,
    annual_rent_yield_pct = EXCLUDED.annual_rent_yield_pct,
    listing_pressure_ratio = EXCLUDED.listing_pressure_ratio,
    discount_pct = EXCLUDED.discount_pct,
    price_momentum_pct = EXCLUDED.price_momentum_pct,
    volume_momentum_pct = EXCLUDED.volume_momentum_pct,
    model_version = EXCLUDED.model_version,
    computed_at = now();
