-- Eurostat median equivalised net income in PPS (dataset: ilc_di03).
-- Country-level income data for purchasing power adjustments.
-- One row per (country_code, year) with median income values.
--
-- Source: data/landing/eurostat/{year}/{month}/ilc_di03.json.gz
-- Format: {"rows": [{"geo_code": "DE", "ref_year": "2022", "value": 23127}, ...]}

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

WITH source AS (
  SELECT unnest(rows) AS r
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/ilc_di03.json.gz',
    auto_detect = true
  )
),
parsed AS (
  SELECT
    UPPER(TRIM(r.geo_code))        AS geo_code,
    CAST(r.ref_year AS INTEGER)    AS ref_year,
    CAST(r.value AS DOUBLE)        AS median_income_pps,
    CURRENT_DATE                   AS extracted_date
  FROM source
  WHERE r.value IS NOT NULL
)
SELECT
  -- Normalise to ISO 3166-1 alpha-2: EL→GR, UK→GB
  @normalize_eurostat_country(geo_code) AS country_code,
  ref_year,
  median_income_pps,
  extracted_date
FROM parsed
WHERE LENGTH(geo_code) = 2
  AND geo_code NOT IN ('EU', 'EA')
  AND median_income_pps > 0
