-- Mart: Canonical school dimension — one row per active URN

-- int_ofsted_latest is only ref()'d inside a conditional block below (it may not
-- exist before the Ofsted pipeline first runs), so declare the dependency
-- explicitly for dbt's DAG.
-- depends_on: {{ ref('int_ofsted_latest') }}

with schools as (
    select * from {{ ref('stg_gias_establishments') }}
)

{% set ofsted_relation = adapter.get_relation(
    database=target.database,
    schema='intermediate',
    identifier='int_ofsted_latest'
) %}

select
    s.urn,
    s.local_authority_code * 1000 + s.establishment_number as laestab,
    s.school_name,
    case
        -- 1. Trust GIAS phase when it's a real value (not the catch-all "Not Applicable")
        when s.phase is not null
             and lower(trim(s.phase)) not in ('not applicable', '', 'unknown')
            then s.phase
        -- 2. Infer from statutory age range (independent schools still publish these)
        when s.statutory_high_age is not null and s.statutory_high_age <= 11 then 'Primary'
        when s.statutory_low_age is not null and s.statutory_low_age >= 11 then 'Secondary'
        when s.statutory_low_age is not null and s.statutory_high_age is not null
             and s.statutory_low_age < 11 and s.statutory_high_age > 11 then 'All-through'
        -- 3. Fallback: infer from school name (covers independents with missing ages)
        when s.school_name ilike '%primary%'
             or s.school_name ilike '%infant%'
             or s.school_name ilike '%junior%'
             or s.school_name ilike '%preparatory%'
             or s.school_name ilike '% prep school%'
             or s.school_name ilike '% prep %'
            then 'Primary'
        when s.school_name ilike '%secondary%'
             or s.school_name ilike '%high school%'
             or s.school_name ilike '%grammar%'
             or s.school_name ilike '%senior school%'
             or s.school_name ilike '%upper school%'
            then 'Secondary'
        -- 4. Give up — leave phase null so the UI renders no pill
        else null
    end as phase,
    s.school_type,
    s.academy_trust_name,
    s.academy_trust_uid,
    s.religious_character,
    s.gender,
    s.statutory_low_age || '-' || s.statutory_high_age as age_range,
    s.capacity,
    s.total_pupils,
    concat_ws(' ', s.head_title, s.head_first_name, s.head_last_name) as headteacher_name,
    s.website,
    s.telephone,
    s.open_date,
    s.close_date,
    s.status,
    s.nursery_provision,
    s.admissions_policy,

    -- Latest Ofsted (populated after monthly Ofsted pipeline runs)
    {% if ofsted_relation is not none %}
    -- Prefer the graded overall effectiveness; fall back to the grade parsed
    -- from the latest ungraded (Section 8) outcome when no graded grade exists.
    coalesce(o.overall_effectiveness, o.ungraded_grade) as ofsted_grade,
    o.inspection_date as ofsted_date,
    o.framework as ofsted_framework
    {% else %}
    null::text as ofsted_grade,
    null::date as ofsted_date,
    null::text as ofsted_framework
    {% endif %}

from schools s
{% if ofsted_relation is not none %}
left join {{ ref('int_ofsted_latest') }} o on s.urn = o.urn
{% endif %}
where s.status = 'Open'
