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

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

latest_ofsted as (
    select * from {{ ref('int_ofsted_latest') }}
)

select
    s.urn,
    s.local_authority_code * 1000 + s.establishment_number as laestab,
    s.school_name,
    s.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
    o.overall_effectiveness as ofsted_grade,
    o.inspection_date as ofsted_date,
    o.framework as ofsted_framework

from schools s
left join latest_ofsted o on s.urn = o.urn
where s.status = 'Open'
