-- Gas prices for non-household consumers (Eurostat nrg_pc_203).
-- EUR/GJ excluding taxes, band GJ1000-9999 (medium-sized commercial consumer).
-- Semi-annual frequency: ref_period is "YYYY-S1" or "YYYY-S2".
--
-- Source: data/landing/eurostat/{year}/{month}/nrg_pc_203.json.gz
-- Format: {"rows": [{"geo_code": "DE", "ref_year": "2024-S1", "value": 14.23}, ...]}

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

WITH source AS (
  SELECT unnest(rows) AS r
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/nrg_pc_203.json.gz',
    auto_detect = true
  )
),
parsed AS (
  SELECT
    UPPER(TRIM(r.geo_code))           AS geo_code,
    TRIM(r.ref_year)                  AS ref_period,
    TRY_CAST(r.value AS DOUBLE)       AS gas_eur_gj
  FROM source
  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,
  ref_period,
  gas_eur_gj
FROM parsed
WHERE LENGTH(geo_code) = 2
  AND geo_code NOT IN ('EU', 'EA', 'EU27_2020')
  AND gas_eur_gj > 0
