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

-- Staging model: Legacy KS2 data from pre-COVID DfE performance tables
-- The tap already maps old column names to match stg_ees_ks2 output;
-- this model just applies safe_numeric casts.

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

    {{ safe_numeric('total_pupils') }}::integer as total_pupils,
    {{ safe_numeric('eligible_pupils') }}::integer as eligible_pupils,

    {{ 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('reading_progress') }} as reading_progress,
    -- Progress CIs / working-towards: not published in the legacy CSVs.
    -- Typed placeholders keep positional alignment with stg_ees_ks2 in
    -- int_ks2_with_lineage's UNION ALL.
    null::numeric as reading_progress_lower_ci,
    null::numeric as reading_progress_upper_ci,
    {{ safe_numeric('writing_expected_pct') }} as writing_expected_pct,
    {{ safe_numeric('writing_high_pct') }} as writing_high_pct,
    {{ safe_numeric('writing_progress') }} as writing_progress,
    null::numeric as writing_progress_lower_ci,
    null::numeric as writing_progress_upper_ci,
    null::numeric as writing_working_towards_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('maths_progress') }} as maths_progress,
    null::numeric as maths_progress_lower_ci,
    null::numeric as maths_progress_upper_ci,
    {{ 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,
    null::numeric as science_expected_pct,

    {{ safe_numeric('reading_absence_pct') }} as reading_absence_pct,
    null::numeric as writing_absence_pct,
    {{ safe_numeric('maths_absence_pct') }} as maths_absence_pct,
    {{ safe_numeric('gps_absence_pct') }} as gps_absence_pct,
    null::numeric as science_absence_pct,

    {{ safe_numeric('rwm_expected_boys_pct') }} as rwm_expected_boys_pct,
    {{ safe_numeric('rwm_high_boys_pct') }} as rwm_high_boys_pct,
    {{ safe_numeric('rwm_expected_girls_pct') }} as rwm_expected_girls_pct,
    {{ safe_numeric('rwm_high_girls_pct') }} as rwm_high_girls_pct,

    {{ safe_numeric('rwm_expected_disadvantaged_pct') }} as rwm_expected_disadvantaged_pct,
    {{ safe_numeric('rwm_expected_non_disadvantaged_pct') }} as rwm_expected_non_disadvantaged_pct,
    {{ safe_numeric('rwm_expected_disadvantaged_pct') }}
        - {{ safe_numeric('rwm_expected_non_disadvantaged_pct') }} as disadvantaged_gap,

    {{ safe_numeric('disadvantaged_pct') }} as disadvantaged_pct,
    {{ safe_numeric('eal_pct') }} as eal_pct,
    {{ safe_numeric('sen_support_pct') }} as sen_support_pct,
    {{ safe_numeric('sen_ehcp_pct') }} as sen_ehcp_pct,
    {{ safe_numeric('stability_pct') }} as stability_pct

from {{ source('raw', 'legacy_ks2') }}
where urn is not null
  and urn ~ '^[0-9]+$'
