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

-- Staging model: KS4 attainment data from EES
-- KS4 performance data is long-format with breakdown dimensions (breakdown_topic,
-- breakdown, sex). Unlike KS2 which has a subject dimension, KS4 metrics are
-- already in separate columns — we just filter to the all-pupils total row.
-- EES uses 'z' (not applicable) and 'c' (confidential) as suppression codes —
-- safe_numeric handles both by treating any non-numeric string as NULL.
-- NOTE: older EES releases (pre-2023/24) use breakdown_topic = 'All pupils';
-- the 2023/24 release switched to breakdown_topic = 'Total'. Both are included.

with performance as (
    select * from {{ source('raw', 'ees_ks4_performance') }}
    where school_urn ~ '^[0-9]+$'
      and time_period ~ '^[0-9]+$'
),

-- Filter to all-pupils totals (one row per school per year)
all_pupils as (
    select
        cast(trim(school_urn) as integer) as urn,
        cast(trim(time_period) as integer) as year,
        {{ safe_numeric('pupil_count') }}::integer as total_pupils,

        -- Attainment 8
        {{ safe_numeric('attainment8_average') }} as attainment_8_score,

        -- Progress 8
        {{ safe_numeric('progress8_average') }} as progress_8_score,
        {{ safe_numeric('progress8_lower_95_ci') }} as progress_8_lower_ci,
        {{ safe_numeric('progress8_upper_95_ci') }} as progress_8_upper_ci,
        {{ safe_numeric('progress8eng_average') }} as progress_8_english,
        {{ safe_numeric('progress8mat_average') }} as progress_8_maths,
        {{ safe_numeric('progress8ebacc_average') }} as progress_8_ebacc,
        {{ safe_numeric('progress8open_average') }} as progress_8_open,

        -- English & Maths pass rates
        {{ safe_numeric('engmath_95_percent') }} as english_maths_strong_pass_pct,
        {{ safe_numeric('engmath_94_percent') }} as english_maths_standard_pass_pct,

        -- EBacc
        {{ safe_numeric('ebacc_entering_percent') }} as ebacc_entry_pct,
        {{ safe_numeric('ebacc_95_percent') }} as ebacc_strong_pass_pct,
        {{ safe_numeric('ebacc_94_percent') }} as ebacc_standard_pass_pct,
        {{ safe_numeric('ebacc_aps_average') }} as ebacc_avg_score,

        -- GCSE grade 9-1
        {{ safe_numeric('gcse_91_percent') }} as gcse_grade_91_pct

    from performance
    where breakdown_topic in ('Total', 'All pupils')
      and breakdown = 'Total'
      and sex = 'Total'
),

-- KS4 info table for context/demographics
info as (
    select
        cast(trim(school_urn) as integer) as urn,
        cast(trim(time_period) as integer) as year,
        {{ safe_numeric('endks4_pupil_count') }}::integer as eligible_pupils,
        {{ safe_numeric('ks2_scaledscore_average') }} as prior_attainment_avg,
        {{ safe_numeric('sen_pupil_percent') }} as sen_pct,
        {{ safe_numeric('sen_with_ehcp_pupil_percent') }} as sen_ehcp_pct,
        {{ safe_numeric('sen_no_ehcp_pupil_percent') }} as sen_support_pct,
        -- EES suppression sentinels (z/c/x/q/u) and blanks must not reach the
        -- mart as banding labels
        case
            when lower(trim(progress8_banding)) in ('', 'z', 'c', 'x', 'q', 'u', 'null')
                then null
            else trim(progress8_banding)
        end as progress_8_banding,
        {{ safe_numeric('attainment8_diffn') }} as attainment_8_disadvantage_gap,
        {{ safe_numeric('progress8_diffn') }} as progress_8_disadvantage_gap
    from {{ source('raw', 'ees_ks4_info') }}
    where school_urn is not null
)

select
    p.urn,
    p.year,
    p.total_pupils,
    i.eligible_pupils,
    i.prior_attainment_avg,

    -- Attainment 8
    p.attainment_8_score,

    -- Progress 8
    p.progress_8_score,
    p.progress_8_lower_ci,
    p.progress_8_upper_ci,
    p.progress_8_english,
    p.progress_8_maths,
    p.progress_8_ebacc,
    p.progress_8_open,

    -- English & Maths
    p.english_maths_strong_pass_pct,
    p.english_maths_standard_pass_pct,

    -- EBacc
    p.ebacc_entry_pct,
    p.ebacc_strong_pass_pct,
    p.ebacc_standard_pass_pct,
    p.ebacc_avg_score,

    -- GCSE
    p.gcse_grade_91_pct,

    -- Context
    i.sen_pct,
    i.sen_ehcp_pct,
    i.sen_support_pct,
    i.progress_8_banding,
    i.attainment_8_disadvantage_gap,
    i.progress_8_disadvantage_gap

from all_pupils p
left join info i on p.urn = i.urn and p.year = i.year
