-- US Census ACS 5-year place-level population.
-- Reads pre-processed landing zone JSON from census_usa extractor.
-- One row per (place_fips, ref_year) — surrogate key is the Census FIPS code.
--
-- Source: data/landing/census_usa/{year}/{month}/acs5_places.json.gz

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

WITH parsed AS (
  SELECT
    row ->> 'city_name'                          AS city_name,
    row ->> 'state_fips'                         AS state_fips,
    row ->> 'place_fips'                         AS place_fips,
    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 || '/census_usa/*/*/acs5_places.json.gz',
      auto_detect = true
    )
  )
  WHERE (row ->> 'place_fips') IS NOT NULL
)
SELECT
  TRIM(city_name)   AS city_name,
  state_fips,
  place_fips,
  population,
  ref_year,
  UPPER(country_code) AS country_code,
  extracted_date
FROM parsed
WHERE population IS NOT NULL
  AND population > 0
  AND place_fips IS NOT NULL
