-- Staging model: Primary and secondary school admissions from EES
-- Wide format, one row per school per year. No geographic_level column.
-- Note: FSM_eligible_percent is stored with mixed case by target-postgres and
-- must be double-quoted in SQL to avoid case-folding to fsm_eligible_percent.

with source as (
    select * from {{ source('raw', 'ees_admissions') }}
    -- Exclude rows where school_urn is null, empty, or non-numeric (e.g. "n/a" LA aggregates)
    where school_urn ~ '^[0-9]+$'
      and time_period ~ '^[0-9]+$'
),

renamed as (
    select
        cast(trim(school_urn) as integer) as urn,
        cast(trim(time_period) as integer) as year,
        school_phase,
        entry_year,

        -- Places and offers
        -- places_offered: number of places the school actually offered in this
        -- year's admissions round. NOT the Published Admission Number (PAN),
        -- which is the school's published capacity — EES does not expose PAN
        -- at school level, so we use the count of offers as the best proxy.
        {{ safe_numeric('total_number_places_offered') }}::integer as places_offered,
        {{ safe_numeric('number_preferred_offers') }}::integer as total_offers,
        {{ safe_numeric('number_1st_preference_offers') }}::integer as first_preference_offers,
        {{ safe_numeric('number_2nd_preference_offers') }}::integer as second_preference_offers,
        {{ safe_numeric('number_3rd_preference_offers') }}::integer as third_preference_offers,

        -- Applications
        {{ safe_numeric('times_put_as_any_preferred_school') }}::integer as total_applications,
        {{ safe_numeric('times_put_as_1st_preference') }}::integer as first_preference_applications,

        -- Cross-borough demand: applications naming this school from families
        -- living in another local authority, and offers made to them.
        {{ safe_numeric('"all_applications_from_another_LA"') }}::integer as cross_la_applications,
        {{ safe_numeric('"offers_to_applicants_from_another_LA"') }}::integer as cross_la_offers,

        -- Proportions
        -- first_preference_offer_pct: of families who listed this school FIRST,
        -- the percentage that received an offer. 0–100 scale.
        -- The raw EES column `proportion_1stprefs_v_totaloffers` stores a
        -- different, misleading figure (first-preference applications divided
        -- by total offers, i.e. an oversubscription ratio) — do not use it.
        case
            when {{ safe_numeric('times_put_as_1st_preference') }} > 0
                then 100.0
                     * {{ safe_numeric('number_1st_preference_offers') }}
                     / {{ safe_numeric('times_put_as_1st_preference') }}
        end as first_preference_offer_pct,

        -- Oversubscription ratio: 1st-preference applications per place offered.
        -- >1 means the school is oversubscribed on first preferences.
        case
            when {{ safe_numeric('total_number_places_offered') }} > 0
                then {{ safe_numeric('times_put_as_1st_preference') }}::numeric
                     / {{ safe_numeric('total_number_places_offered') }}
        end as oversubscription_ratio,

        -- Derived: oversubscribed if 1st-preference applications > places offered
        -- Use already-cast columns to avoid repeating the regex expression
        (
            {{ safe_numeric('times_put_as_1st_preference') }}
            > {{ safe_numeric('total_number_places_offered') }}
        ) as oversubscribed,

        -- Context
        admissions_policy,
        {{ safe_numeric('"FSM_eligible_percent"') }} as fsm_eligible_pct

    from source
)

select * from renamed
