-- Eurostat Urban Audit city population (dataset: urb_cpop1).
-- Reads pre-processed landing zone JSON (extractor normalizes JSON-stat to flat rows).
-- One row per (city_code, year) with validated population values.
--
-- Source: data/landing/eurostat/{year}/{month}/urb_cpop1.json.gz

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

WITH parsed AS (
  SELECT
    row ->> 'geo_code'           AS geo_code,
    row ->> 'ref_year'           AS ref_year,
    TRY_CAST(row ->> 'value' AS DOUBLE) AS population,
    filename                     AS source_file,
    CURRENT_DATE                 AS extracted_date
  FROM (
    SELECT UNNEST(rows) AS row, filename
    FROM read_json(
      @LANDING_DIR || '/eurostat/*/*/urb_cpop1.json.gz',
      format = 'auto',
      filename = true
    )
  )
  WHERE (row ->> 'geo_code') IS NOT NULL
)
SELECT
  UPPER(geo_code)                AS city_code,
  UPPER(LEFT(geo_code, 2))       AS country_code,
  ref_year::INTEGER              AS ref_year,
  population::BIGINT             AS population,
  extracted_date
FROM parsed
WHERE population IS NOT NULL
  AND population > 0
  AND ref_year ~ '^\d{4}$'
