-- ONS 2021 Census population by Local Authority District (LAD).
-- Reads pre-processed landing zone JSON from ons_uk extractor.
-- One row per (lad_code, ref_year) — LAD code is the ONS area identifier.
--
-- Source: data/landing/ons_uk/{year}/{month}/lad_population.json.gz

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

WITH parsed AS (
  SELECT
    row ->> 'lad_code'                           AS lad_code,
    row ->> 'lad_name'                           AS lad_name,
    TRY_CAST(row ->> 'population' AS BIGINT)     AS population,
    TRY_CAST(row ->> 'ref_year'   AS INTEGER)    AS ref_year,
    row ->> 'country_code'                       AS country_code,
    CURRENT_DATE                                 AS extracted_date
  FROM (
    SELECT UNNEST(rows) AS row
    FROM read_json(
      @LANDING_DIR || '/ons_uk/*/*/lad_population.json.gz',
      auto_detect = true
    )
  )
  WHERE (row ->> 'lad_code') IS NOT NULL
)
SELECT
  UPPER(TRIM(lad_code)) AS lad_code,
  TRIM(lad_name)        AS lad_name,
  population,
  ref_year,
  UPPER(country_code)   AS country_code,
  extracted_date
FROM parsed
WHERE population IS NOT NULL
  AND population > 0
  AND lad_code IS NOT NULL
