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

-- Staging model: KS2 attainment + information
-- Single-pass pivot: one GROUP BY scan of 1.2M rows handles all subjects
-- and all breakdowns (All pupils, Boys, Girls, Disadvantaged, Not disadvantaged)
-- instead of 5 separate CTE scans + 5 LEFT JOINs.
-- EES uses 'z' (not applicable) and 'c' (confidential) as suppression codes —
-- safe_numeric handles both by treating any non-numeric string as NULL.

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

pivoted as (
    select
        cast(trim(school_urn) as integer) as urn,
        cast(trim(time_period) as integer) as year,

        -- Reading, Writing & Maths combined (All pupils / Total)
        max(case when subject = 'Reading, writing and maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as rwm_expected_pct,
        max(case when subject = 'Reading, writing and maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as rwm_high_pct,

        -- Reading (All pupils / Total)
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as reading_expected_pct,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as reading_high_pct,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('average_scaled_score') }} end) as reading_avg_score,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_score') }} end) as reading_progress,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_lower_conf_interval') }} end) as reading_progress_lower_ci,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_upper_conf_interval') }} end) as reading_progress_upper_ci,
        max(case when subject = 'Reading'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('absent_or_not_able_to_access_percent') }} end) as reading_absence_pct,

        -- Writing (All pupils / Total)
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as writing_expected_pct,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as writing_high_pct,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_score') }} end) as writing_progress,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_lower_conf_interval') }} end) as writing_progress_lower_ci,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_upper_conf_interval') }} end) as writing_progress_upper_ci,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('working_towards_expected_standard_pupil_percent') }} end) as writing_working_towards_pct,
        max(case when subject = 'Writing'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('absent_or_not_able_to_access_percent') }} end) as writing_absence_pct,

        -- Maths (All pupils / Total)
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as maths_expected_pct,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as maths_high_pct,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('average_scaled_score') }} end) as maths_avg_score,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_score') }} end) as maths_progress,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_lower_conf_interval') }} end) as maths_progress_lower_ci,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('progress_measure_upper_conf_interval') }} end) as maths_progress_upper_ci,
        max(case when subject = 'Maths'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('absent_or_not_able_to_access_percent') }} end) as maths_absence_pct,

        -- GPS (All pupils / Total)
        max(case when (subject ilike '%grammar%' or subject = 'GPS')
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as gps_expected_pct,
        max(case when (subject ilike '%grammar%' or subject = 'GPS')
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as gps_high_pct,
        max(case when (subject ilike '%grammar%' or subject = 'GPS')
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('average_scaled_score') }} end) as gps_avg_score,
        max(case when (subject ilike '%grammar%' or subject = 'GPS')
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('absent_or_not_able_to_access_percent') }} end) as gps_absence_pct,

        -- Science (All pupils / Total)
        max(case when subject = 'Science'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as science_expected_pct,
        max(case when subject = 'Science'
                  and breakdown_topic = 'All pupils' and breakdown = 'Total'
            then {{ safe_numeric('absent_or_not_able_to_access_percent') }} end) as science_absence_pct,

        -- Gender breakdown for Reading, Writing & Maths
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Boys'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as rwm_expected_boys_pct,
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Boys'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as rwm_high_boys_pct,
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Girls'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as rwm_expected_girls_pct,
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Girls'
            then {{ safe_numeric('higher_standard_pupil_percent') }} end) as rwm_high_girls_pct,

        -- Disadvantaged breakdown for Reading, Writing & Maths
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Disadvantaged'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as rwm_expected_disadvantaged_pct,
        max(case when subject = 'Reading, writing and maths' and breakdown = 'Not disadvantaged'
            then {{ safe_numeric('expected_standard_pupil_percent') }} end) as rwm_expected_non_disadvantaged_pct

    from attainment
    group by school_urn, time_period
),

info as (
    select
        cast(trim(school_urn) as integer) as urn,
        cast(trim(time_period) as integer) as year,
        {{ safe_numeric('totpups') }}::integer as total_pupils,
        {{ safe_numeric('telig') }}::integer as eligible_pupils,
        {{ safe_numeric('ptfsm6cla1a') }} as disadvantaged_pct,
        {{ safe_numeric('ptealgrp2') }} as eal_pct,
        {{ safe_numeric('psenelk') }} as sen_support_pct,
        {{ safe_numeric('psenele') }} as sen_ehcp_pct,
        {{ safe_numeric('ptmobn') }} as stability_pct
    from {{ source('raw', 'ees_ks2_info') }}
    where school_urn is not null
)

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

    p.rwm_expected_pct,
    p.rwm_high_pct,
    p.reading_expected_pct,
    p.reading_high_pct,
    p.reading_avg_score,
    p.reading_progress,
    p.reading_progress_lower_ci,
    p.reading_progress_upper_ci,
    p.writing_expected_pct,
    p.writing_high_pct,
    p.writing_progress,
    p.writing_progress_lower_ci,
    p.writing_progress_upper_ci,
    p.writing_working_towards_pct,
    p.maths_expected_pct,
    p.maths_high_pct,
    p.maths_avg_score,
    p.maths_progress,
    p.maths_progress_lower_ci,
    p.maths_progress_upper_ci,
    p.gps_expected_pct,
    p.gps_high_pct,
    p.gps_avg_score,
    p.science_expected_pct,

    p.reading_absence_pct,
    p.writing_absence_pct,
    p.maths_absence_pct,
    p.gps_absence_pct,
    p.science_absence_pct,

    p.rwm_expected_boys_pct,
    p.rwm_high_boys_pct,
    p.rwm_expected_girls_pct,
    p.rwm_high_girls_pct,

    p.rwm_expected_disadvantaged_pct,
    p.rwm_expected_non_disadvantaged_pct,
    p.rwm_expected_disadvantaged_pct - p.rwm_expected_non_disadvantaged_pct as disadvantaged_gap,

    i.disadvantaged_pct,
    i.eal_pct,
    i.sen_support_pct,
    i.sen_ehcp_pct,
    i.stability_pct

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