-- Price level indices relative to EU27=100 (Eurostat prc_ppp_ind).
-- Five categories, each from a separate landing file (different ppp_cat filters).
-- Annual frequency.
--
-- Categories and what they scale in the calculator:
--   construction — CAPEX: hallCostSqm, foundationSqm, hvac, electrical, sanitary, etc.
--   housing      — rentSqm, landPriceSqm, water, outdoorRent
--   services     — cleaning, maintenance, marketing
--   misc         — insurance
--   government   — permitsCompliance, propertyTax
--
-- Sources:
--   data/landing/eurostat/*/*/prc_ppp_ind_construction.json.gz  (ppp_cat: A050202)
--   data/landing/eurostat/*/*/prc_ppp_ind_housing.json.gz       (ppp_cat: A0104)
--   data/landing/eurostat/*/*/prc_ppp_ind_services.json.gz      (ppp_cat: P0201)
--   data/landing/eurostat/*/*/prc_ppp_ind_misc.json.gz          (ppp_cat: A0112)
--   data/landing/eurostat/*/*/prc_ppp_ind_government.json.gz    (ppp_cat: P0202)
--
-- Format: {"rows": [{"geo_code": "DE", "ref_year": "2022", "value": 107.3}, ...]}

MODEL (
  name staging.stg_price_levels,
  kind FULL,
  cron '@daily',
  grain (country_code, category, ref_year)
);

WITH construction_raw AS (
  SELECT unnest(rows) AS r, 'construction' AS category
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/prc_ppp_ind_construction.json.gz',
    auto_detect = true
  )
),
housing_raw AS (
  SELECT unnest(rows) AS r, 'housing' AS category
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/prc_ppp_ind_housing.json.gz',
    auto_detect = true
  )
),
services_raw AS (
  SELECT unnest(rows) AS r, 'services' AS category
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/prc_ppp_ind_services.json.gz',
    auto_detect = true
  )
),
misc_raw AS (
  SELECT unnest(rows) AS r, 'misc' AS category
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/prc_ppp_ind_misc.json.gz',
    auto_detect = true
  )
),
government_raw AS (
  SELECT unnest(rows) AS r, 'government' AS category
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/prc_ppp_ind_government.json.gz',
    auto_detect = true
  )
),
all_raw AS (
  SELECT r, category FROM construction_raw
  UNION ALL
  SELECT r, category FROM housing_raw
  UNION ALL
  SELECT r, category FROM services_raw
  UNION ALL
  SELECT r, category FROM misc_raw
  UNION ALL
  SELECT r, category FROM government_raw
),
parsed AS (
  SELECT
    UPPER(TRIM(r.geo_code))           AS geo_code,
    TRY_CAST(r.ref_year AS INTEGER)   AS ref_year,
    TRY_CAST(r.value AS DOUBLE)       AS pli,
    category
  FROM all_raw
  WHERE r.value IS NOT NULL
)
SELECT
  -- Normalise to ISO 3166-1 alpha-2: EL→GR, UK→GB
  CASE geo_code
    WHEN 'EL' THEN 'GR'
    WHEN 'UK' THEN 'GB'
    ELSE geo_code
  END                                 AS country_code,
  category,
  ref_year,
  pli
FROM parsed
WHERE LENGTH(geo_code) = 2
  AND geo_code NOT IN ('EU', 'EA', 'EU27_2020')
  AND pli > 0
