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

-- Mart: state-school context benchmarks from the pupil census — one row per
-- phase, latest census year. Computed at import time (never per request).
-- fsm_pct / eal_pct are pupil-weighted means, i.e. "what % of pupils", not
-- "the median school" — this matches how DfE quotes national FSM/EAL rates.
-- Consumers must label these "state-school average (computed from our
-- dataset)" (spec §8.6), never "England average".

with latest as (
    select max(year) as year from {{ ref('fact_pupil_characteristics') }}
),

classified as (
    select
        case
            when p.phase_type_grouping ilike '%primary%'   then 'primary'
            when p.phase_type_grouping ilike '%secondary%' then 'secondary'
        end as phase,
        p.total_pupils,
        p.fsm_pct,
        p.eal_pct,
        l.year
    from {{ ref('fact_pupil_characteristics') }} p
    join latest l on p.year = l.year
    where p.total_pupils is not null and p.total_pupils > 0
)

select
    phase,
    max(year) as year,
    round((sum(fsm_pct * total_pupils) filter (where fsm_pct is not null)
        / nullif(sum(total_pupils) filter (where fsm_pct is not null), 0))::numeric, 1) as fsm_pct,
    round((sum(eal_pct * total_pupils) filter (where eal_pct is not null)
        / nullif(sum(total_pupils) filter (where eal_pct is not null), 0))::numeric, 1) as eal_pct,
    round(percentile_cont(0.5) within group (order by total_pupils))::integer as median_pupils
from classified
where phase is not null
group by phase
