-- World Bank WDI indicators: GNI per capita PPP and price level ratio.
-- Pivoted to one row per (country_code, ref_year) with both indicators as columns.
--
-- Source: data/landing/worldbank/{year}/{month}/wdi_indicators.json.gz
-- Extracted by: worldbank.py
-- Used by: dim_countries (fallback behind Eurostat for non-EU countries)

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

WITH parsed AS (
  SELECT
    row ->> 'country_code'                          AS country_code,
    TRY_CAST(row ->> 'ref_year' AS INTEGER)         AS ref_year,
    row ->> 'indicator'                             AS indicator,
    TRY_CAST(row ->> 'value' AS DOUBLE)             AS value,
    CURRENT_DATE                                    AS extracted_date
  FROM (
    SELECT UNNEST(rows) AS row
    FROM read_json(
      @LANDING_DIR || '/worldbank/*/*/wdi_indicators.json.gz',
      auto_detect = true
    )
  )
  WHERE (row ->> 'country_code') IS NOT NULL
)
SELECT
  country_code,
  ref_year,
  MAX(value) FILTER (WHERE indicator = 'NY.GNP.PCAP.PP.CD') AS gni_ppp,
  MAX(value) FILTER (WHERE indicator = 'PA.NUS.PPPC.RF')     AS price_level_ratio,
  MAX(extracted_date)                                         AS extracted_date
FROM parsed
WHERE value IS NOT NULL
  AND value > 0
  AND LENGTH(country_code) = 2
GROUP BY country_code, ref_year
