-- Intermediate model: KS4 data chained across academy conversions
-- Unions EES (2023/24 onwards) and legacy (2015/16–2018/19) school-level data

with all_ks4 as (
    select * from {{ ref('stg_ees_ks4') }}
    union all
    select * from {{ ref('stg_legacy_ks4') }}
),

current_ks4 as (
    select
        urn as current_urn,
        urn as source_urn,
        year, total_pupils, eligible_pupils, prior_attainment_avg,
        attainment_8_score,
        progress_8_score, progress_8_lower_ci, progress_8_upper_ci,
        progress_8_english, progress_8_maths, progress_8_ebacc, progress_8_open,
        english_maths_strong_pass_pct, english_maths_standard_pass_pct,
        ebacc_entry_pct, ebacc_strong_pass_pct, ebacc_standard_pass_pct, ebacc_avg_score,
        gcse_grade_91_pct,
        sen_pct, sen_support_pct, sen_ehcp_pct,
        progress_8_banding, attainment_8_disadvantage_gap, progress_8_disadvantage_gap
    from all_ks4
),

predecessor_ks4 as (
    -- If multiple predecessors have data for the same year, keep the one with most pupils.
    select distinct on (lin.current_urn, ks4.year)
        lin.current_urn,
        ks4.urn as source_urn,
        ks4.year, ks4.total_pupils, ks4.eligible_pupils, ks4.prior_attainment_avg,
        ks4.attainment_8_score,
        ks4.progress_8_score, ks4.progress_8_lower_ci, ks4.progress_8_upper_ci,
        ks4.progress_8_english, ks4.progress_8_maths, ks4.progress_8_ebacc, ks4.progress_8_open,
        ks4.english_maths_strong_pass_pct, ks4.english_maths_standard_pass_pct,
        ks4.ebacc_entry_pct, ks4.ebacc_strong_pass_pct, ks4.ebacc_standard_pass_pct, ks4.ebacc_avg_score,
        ks4.gcse_grade_91_pct,
        ks4.sen_pct, ks4.sen_support_pct, ks4.sen_ehcp_pct,
        ks4.progress_8_banding, ks4.attainment_8_disadvantage_gap, ks4.progress_8_disadvantage_gap
    from all_ks4 ks4
    inner join {{ ref('int_school_lineage') }} lin
        on ks4.urn = lin.predecessor_urn
    where not exists (
        select 1 from all_ks4 curr
        where curr.urn = lin.current_urn
          and curr.year = ks4.year
    )
    order by lin.current_urn, ks4.year, ks4.total_pupils desc nulls last
),

combined as (
    select * from current_ks4
    union all
    select * from predecessor_ks4
)

select * from combined
