-- Mart: Merged KS2 + KS4 performance — one row per URN per year
-- FULL OUTER JOIN so all-through schools have both KS2 and KS4 columns populated.
-- Pure primary schools have NULL KS4 columns; pure secondary schools have NULL KS2 columns.

with ks2 as (
    select * from {{ ref('fact_ks2_performance') }}
),

ks4 as (
    select * from {{ ref('fact_ks4_performance') }}
)

select
    -- Join keys (coalesced so neither side is dropped)
    coalesce(ks2.urn, ks4.urn)                     as urn,
    coalesce(ks2.source_urn, ks4.source_urn)        as source_urn,
    coalesce(ks2.year, ks4.year)                    as year,
    coalesce(ks2.total_pupils, ks4.total_pupils)    as total_pupils,
    coalesce(ks2.eligible_pupils, ks4.eligible_pupils) as eligible_pupils,

    -- KS2 columns (NULL for pure secondary schools)
    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.stability_pct,

    -- KS4 columns (NULL for pure primary schools)
    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.progress_8_banding,
    ks4.attainment_8_disadvantage_gap,
    ks4.progress_8_disadvantage_gap,
    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.prior_attainment_avg,
    ks4.sen_pct,

    -- Shared SEN columns — KS2 preferred, fall back to KS4
    coalesce(ks2.sen_support_pct, ks4.sen_support_pct) as sen_support_pct,
    coalesce(ks2.sen_ehcp_pct,    ks4.sen_ehcp_pct)    as sen_ehcp_pct

from ks2
full outer join ks4
    on ks2.urn = ks4.urn
    and ks2.year = ks4.year
