-- Staging model: Ofsted inspection records
-- Handles both OEIF (pre-Nov 2025) and Report Card (post-Nov 2025) frameworks

with source as (
    select * from {{ source('raw', 'ofsted_inspections') }}
),

renamed as (
    select
        cast(urn as integer)                                                    as urn,
        to_date(nullif(trim(inspection_date), 'NULL'), 'DD/MM/YYYY')             as inspection_date,
        inspection_type,
        event_type_grouping                                                     as framework,

        -- OEIF grades (1-4 scale)
        {{ safe_numeric('overall_effectiveness') }}::integer                    as overall_effectiveness,
        {{ safe_numeric('quality_of_education') }}::integer                     as quality_of_education,
        {{ safe_numeric('behaviour_and_attitudes') }}::integer                  as behaviour_attitudes,
        {{ safe_numeric('personal_development') }}::integer                     as personal_development,
        {{ safe_numeric('effectiveness_of_leadership_and_management') }}::integer as leadership_management,
        {{ safe_numeric('early_years_provision') }}::integer                    as early_years_provision,
        {{ safe_numeric('sixth_form_provision') }}::integer                     as sixth_form_provision,

        -- Report Card fields (post-Nov 2025 framework)
        -- TODO: add rc_* columns to tap-uk-ofsted schema once CSV column names are confirmed
        null::text as rc_safeguarding_met,
        null::text as rc_inclusion,
        null::text as rc_curriculum_teaching,
        null::text as rc_achievement,
        null::text as rc_attendance_behaviour,
        null::text as rc_personal_development,
        null::text as rc_leadership_governance,
        null::text as rc_early_years,
        null::text as rc_sixth_form,

        report_url
    from source
    where urn is not null
      and nullif(trim(inspection_date), 'NULL') is not null
)

select * from renamed
where inspection_date is not null
