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

-- Staging model: School Pupils and Characteristics (census)
-- One row per school per year. CSV has 269 columns; we extract the
-- aggregate demographic summaries only (not per-age/year-group breakdowns).
-- Column renames happen in the tap before Singer persists to raw.ees_census.

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

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

    -- Pupil counts
    {{ safe_numeric('total_pupils') }}::integer      as total_pupils,
    {{ safe_numeric('female_pupils') }}::integer     as female_pupils,
    {{ safe_numeric('male_pupils') }}::integer       as male_pupils,
    {{ safe_numeric('full_time_pupils') }}::integer  as full_time_pupils,
    {{ safe_numeric('part_time_pupils') }}::integer  as part_time_pupils,
    {{ safe_numeric('total_boarders') }}::integer    as total_boarders,

    -- FSM
    {{ safe_numeric('fsm_eligible_n') }}::integer    as fsm_eligible_n,
    {{ safe_numeric('fsm_pct') }}                    as fsm_pct,

    -- EAL & young carers
    {{ safe_numeric('eal_pct') }}                    as eal_pct,
    {{ safe_numeric('young_carer_pct') }}            as young_carer_pct,

    -- Ethnicity
    {{ safe_numeric('ethnicity_white_british_pct') }}     as ethnicity_white_british_pct,
    {{ safe_numeric('ethnicity_white_other_pct') }}       as ethnicity_white_other_pct,
    {{ safe_numeric('ethnicity_gypsy_roma_pct') }}        as ethnicity_gypsy_roma_pct,
    {{ safe_numeric('ethnicity_mixed_wbc_pct') }}         as ethnicity_mixed_wbc_pct,
    {{ safe_numeric('ethnicity_mixed_wba_pct') }}         as ethnicity_mixed_wba_pct,
    {{ safe_numeric('ethnicity_mixed_wa_pct') }}          as ethnicity_mixed_wa_pct,
    {{ safe_numeric('ethnicity_mixed_other_pct') }}       as ethnicity_mixed_other_pct,
    {{ safe_numeric('ethnicity_asian_indian_pct') }}      as ethnicity_asian_indian_pct,
    {{ safe_numeric('ethnicity_asian_pakistani_pct') }}   as ethnicity_asian_pakistani_pct,
    {{ safe_numeric('ethnicity_asian_bangladeshi_pct') }} as ethnicity_asian_bangladeshi_pct,
    {{ safe_numeric('ethnicity_asian_other_pct') }}       as ethnicity_asian_other_pct,
    {{ safe_numeric('ethnicity_black_caribbean_pct') }}   as ethnicity_black_caribbean_pct,
    {{ safe_numeric('ethnicity_black_african_pct') }}     as ethnicity_black_african_pct,
    {{ safe_numeric('ethnicity_black_other_pct') }}       as ethnicity_black_other_pct,
    {{ safe_numeric('ethnicity_chinese_pct') }}           as ethnicity_chinese_pct,
    {{ safe_numeric('ethnicity_other_pct') }}             as ethnicity_other_pct,
    {{ safe_numeric('ethnicity_unclassified_pct') }}      as ethnicity_unclassified_pct

from source
