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

MODEL (
  name staging.stg_electricity_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_205.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 electricity_eur_kwh
  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,
  electricity_eur_kwh
FROM parsed
WHERE LENGTH(geo_code) = 2
  AND geo_code NOT IN ('EU', 'EA', 'EU27_2020')
  AND electricity_eur_kwh > 0
