-- Labour cost levels EUR/hour (Eurostat lc_lci_lev).
-- NACE R2 sector N (administrative and support service activities).
-- D1_D2_A_HW structure: wages + non-wage costs, actual hours worked.
-- Annual frequency.
--
-- Stored for future "staffed scenario" calculator variant.
-- Not wired into default calculator overrides (staff=0 is a business assumption).
--
-- Source: data/landing/eurostat/{year}/{month}/lc_lci_lev.json.gz
-- Format: {"rows": [{"geo_code": "DE", "ref_year": "2022", "value": 28.4}, ...]}

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

WITH source AS (
  SELECT unnest(rows) AS r
  FROM read_json(
    @LANDING_DIR || '/eurostat/*/*/lc_lci_lev.json.gz',
    auto_detect = true
  )
),
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 labour_cost_eur_hour
  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_year,
  labour_cost_eur_hour
FROM parsed
WHERE LENGTH(geo_code) = 2
  AND geo_code NOT IN ('EU', 'EA', 'EU27_2020')
  AND labour_cost_eur_hour > 0
