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

-- Staging model: official DfE KS4 national headline averages — one row per
-- academic year (England, all state-funded, all pupils). Source: EES data
-- catalogue "National characteristics summary data". Suppressed values
-- ('z', 'x') are coerced to NULL by safe_numeric — Progress 8 is 'z' in
-- years with no KS2 baseline (e.g. 2024/25): legitimately unpublished.

select
    cast(trim(time_period) as integer)                        as year,
    {{ safe_numeric('attainment_8_score') }}                  as attainment_8_score,
    {{ safe_numeric('progress_8_score') }}                    as progress_8_score,
    {{ safe_numeric('english_maths_standard_pass_pct') }}     as english_maths_standard_pass_pct,
    {{ safe_numeric('english_maths_strong_pass_pct') }}       as english_maths_strong_pass_pct,
    {{ safe_numeric('ebacc_entry_pct') }}                     as ebacc_entry_pct,
    {{ safe_numeric('ebacc_standard_pass_pct') }}             as ebacc_standard_pass_pct,
    {{ safe_numeric('ebacc_strong_pass_pct') }}               as ebacc_strong_pass_pct,
    {{ safe_numeric('ebacc_avg_score') }}                     as ebacc_avg_score
from {{ source('raw', 'ees_ks4_national') }}
where time_period ~ '^[0-9]+$'
