-- Eurostat NUTS-1 and NUTS-2 regional household income in PPS (dataset: nama_10r_2hhinc).
-- Accepts NUTS-1 codes (3 characters, e.g. DE1) and NUTS-2 codes (4 characters, e.g. DE21).
-- One row per (nuts_code, ref_year).
--
-- Source: data/landing/eurostat/{year}/{month}/nama_10r_2hhinc.json.gz
-- Format: {"rows": [{"geo_code": "DE21", "ref_year": "2022", "value": 31200}, ...]}

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

WITH source AS (
  SELECT unnest(rows) AS r
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/nama_10r_2hhinc.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 regional_income_pps,
    CURRENT_DATE                   AS extracted_date
  FROM source
  WHERE r.value IS NOT NULL
)
SELECT
  -- Normalise to ISO 3166-1 alpha-2 prefix: EL→GR, UK→GB
  @normalize_eurostat_nuts(geo_code) AS nuts_code,
  -- NUTS level: 3-char = NUTS-1, 4-char = NUTS-2
  LENGTH(geo_code) - 2             AS nuts_level,
  ref_year,
  regional_income_pps,
  extracted_date
FROM parsed
-- NUTS-1 (3 chars) and NUTS-2 (4 chars); exclude country codes (2) and NUTS-3 (5)
WHERE LENGTH(geo_code) IN (3, 4)
  AND regional_income_pps > 0
