{{ config(materialized='table') }}

-- Staging model: DfE KS2 national headline averages
-- Source: EES data catalogue CSV (one row per academic year, England national total)
-- COVID years 2019/20 and 2020/21 are naturally absent — DfE did not publish figures
-- because national assessments were cancelled. Those years produce no rows here.
-- 'x' (not applicable) and suppressed values are coerced to NULL by safe_numeric.

select
    cast(trim(time_period) as integer)          as year,

    {{ safe_numeric('rwm_expected_pct') }}       as rwm_expected_pct,
    {{ safe_numeric('rwm_high_pct') }}           as rwm_high_pct,

    {{ safe_numeric('reading_expected_pct') }}   as reading_expected_pct,
    {{ safe_numeric('reading_high_pct') }}       as reading_high_pct,
    {{ safe_numeric('reading_avg_score') }}      as reading_avg_score,

    {{ safe_numeric('writing_expected_pct') }}   as writing_expected_pct,
    {{ safe_numeric('writing_gd_pct') }}         as writing_gd_pct,

    {{ safe_numeric('maths_expected_pct') }}     as maths_expected_pct,
    {{ safe_numeric('maths_high_pct') }}         as maths_high_pct,
    {{ safe_numeric('maths_avg_score') }}        as maths_avg_score,

    {{ safe_numeric('gps_expected_pct') }}       as gps_expected_pct,
    {{ safe_numeric('gps_high_pct') }}           as gps_high_pct,
    {{ safe_numeric('gps_avg_score') }}          as gps_avg_score,

    {{ safe_numeric('science_expected_pct') }}   as science_expected_pct

from {{ source('raw', 'ees_ks2_national') }}
where time_period ~ '^[0-9]+$'
  -- 2015/16 was the first year of the current expected-standard tests, so it's
  -- the correct floor (not 2016/17 -- that excluded a real, comparable national
  -- row). GPS/science/scaled-score columns are already mapped correctly end to
  -- end (tap.py's _KS2_NATIONAL_COL_MAP + this model select them fine); the
  -- prod NULLs for those fields are stale raw.ees_ks2_national data from before
  -- the map covered them, not a mapping bug -- no map change accompanies this fix.
  and cast(trim(time_period) as integer) >= 201516
