-- US Census ACS 5-year state-level median household income (B19013_001E).
-- One row per (state_fips, ref_year). Income stored in nominal USD.
-- PPS conversion to EU-comparable scale happens in dim_locations.
--
-- Source: data/landing/census_usa/{year}/{month}/acs5_state_income.json.gz
-- Extracted by: census_usa_income.py

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

WITH parsed AS (
  SELECT
    row ->> 'state_fips'                             AS state_fips,
    row ->> 'state_name'                             AS state_name,
    TRY_CAST(row ->> 'median_income_usd' AS DOUBLE)  AS median_income_usd,
    TRY_CAST(row ->> 'ref_year'          AS INTEGER) AS ref_year,
    CURRENT_DATE                                     AS extracted_date
  FROM (
    SELECT UNNEST(rows) AS row
    FROM read_json(
      @LANDING_DIR || '/census_usa/*/*/acs5_state_income.json.gz',
      auto_detect = true
    )
  )
  WHERE (row ->> 'state_fips') IS NOT NULL
)
SELECT
  state_fips,
  TRIM(state_name)    AS state_name,
  median_income_usd,
  ref_year,
  extracted_date
FROM parsed
WHERE median_income_usd IS NOT NULL
  AND median_income_usd > 0
