-- Intermediate model: KS2 data chained across academy conversions
-- Maps predecessor URN data to the current active URN
-- Unions EES (2022+) and legacy (2015-2019) school-level data

with all_ks2 as (
    select * from {{ ref('stg_ees_ks2') }}
    union all
    select * from {{ ref('stg_legacy_ks2') }}
),

current_ks2 as (
    select
        urn as current_urn,
        urn as source_urn,
        year, total_pupils, eligible_pupils,
        rwm_expected_pct, rwm_high_pct,
        reading_expected_pct, reading_high_pct, reading_avg_score, reading_progress,
        reading_progress_lower_ci, reading_progress_upper_ci,
        writing_expected_pct, writing_high_pct, writing_progress,
        writing_progress_lower_ci, writing_progress_upper_ci, writing_working_towards_pct,
        maths_expected_pct, maths_high_pct, maths_avg_score, maths_progress,
        maths_progress_lower_ci, maths_progress_upper_ci,
        gps_expected_pct, gps_high_pct, gps_avg_score, science_expected_pct,
        reading_absence_pct, writing_absence_pct, maths_absence_pct, gps_absence_pct, science_absence_pct,
        rwm_expected_boys_pct, rwm_high_boys_pct, rwm_expected_girls_pct, rwm_high_girls_pct,
        rwm_expected_disadvantaged_pct, rwm_expected_non_disadvantaged_pct, disadvantaged_gap,
        disadvantaged_pct, eal_pct, sen_support_pct, sen_ehcp_pct, stability_pct
    from all_ks2
),

predecessor_ks2 as (
    -- If multiple predecessors have data for the same year, keep the one with most pupils.
    select distinct on (lin.current_urn, ks2.year)
        lin.current_urn,
        ks2.urn as source_urn,
        ks2.year, ks2.total_pupils, ks2.eligible_pupils,
        ks2.rwm_expected_pct, ks2.rwm_high_pct,
        ks2.reading_expected_pct, ks2.reading_high_pct, ks2.reading_avg_score, ks2.reading_progress,
        ks2.reading_progress_lower_ci, ks2.reading_progress_upper_ci,
        ks2.writing_expected_pct, ks2.writing_high_pct, ks2.writing_progress,
        ks2.writing_progress_lower_ci, ks2.writing_progress_upper_ci, ks2.writing_working_towards_pct,
        ks2.maths_expected_pct, ks2.maths_high_pct, ks2.maths_avg_score, ks2.maths_progress,
        ks2.maths_progress_lower_ci, ks2.maths_progress_upper_ci,
        ks2.gps_expected_pct, ks2.gps_high_pct, ks2.gps_avg_score, ks2.science_expected_pct,
        ks2.reading_absence_pct, ks2.writing_absence_pct, ks2.maths_absence_pct, ks2.gps_absence_pct, ks2.science_absence_pct,
        ks2.rwm_expected_boys_pct, ks2.rwm_high_boys_pct, ks2.rwm_expected_girls_pct, ks2.rwm_high_girls_pct,
        ks2.rwm_expected_disadvantaged_pct, ks2.rwm_expected_non_disadvantaged_pct, ks2.disadvantaged_gap,
        ks2.disadvantaged_pct, ks2.eal_pct, ks2.sen_support_pct, ks2.sen_ehcp_pct, ks2.stability_pct
    from all_ks2 ks2
    inner join {{ ref('int_school_lineage') }} lin
        on ks2.urn = lin.predecessor_urn
    where not exists (
        select 1 from all_ks2 curr
        where curr.urn = lin.current_urn
          and curr.year = ks2.year
    )
    order by lin.current_urn, ks2.year, ks2.total_pupils desc nulls last
),

combined as (
    select * from current_ks2
    union all
    select * from predecessor_ks2
)

select * from combined
