-- 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,
        -- Inspection event date: the graded inspection when present, otherwise the
        -- ungraded (Section 8) inspection so schools with only an ungraded
        -- inspection are still retained.
        coalesce(
            to_date(nullif(trim(inspection_date), 'NULL'), 'DD/MM/YYYY'),
            to_date(nullif(trim(ungraded_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,

        -- Ungraded (Section 8) inspection outcome — free text, plus a grade
        -- parsed from it (1/2/null) used as a last-resort fallback for schools
        -- with no graded overall effectiveness.
        nullif(trim(ungraded_outcome), 'NULL')                                  as ungraded_outcome,
        {{ parse_ungraded_outcome('ungraded_outcome') }}::integer               as ungraded_grade,

        -- Report Card fields (post-Nov 2025 framework), 5-point scale:
        -- 1 Exceptional · 2 Strong standard · 3 Expected standard
        -- · 4 Needs attention · 5 Urgent improvement
        case lower(trim(nullif(rc_safeguarding_met, 'NULL')))
            when 'met' then true
            when 'not met' then false
        end                                                               as rc_safeguarding_met,
        {{ parse_report_card_grade('rc_inclusion') }}::integer            as rc_inclusion,
        {{ parse_report_card_grade('rc_curriculum_teaching') }}::integer  as rc_curriculum_teaching,
        {{ parse_report_card_grade('rc_achievement') }}::integer          as rc_achievement,
        {{ parse_report_card_grade('rc_attendance_behaviour') }}::integer as rc_attendance_behaviour,
        {{ parse_report_card_grade('rc_personal_development') }}::integer as rc_personal_development,
        {{ parse_report_card_grade('rc_leadership_governance') }}::integer as rc_leadership_governance,
        {{ parse_report_card_grade('rc_early_years') }}::integer          as rc_early_years,
        {{ parse_report_card_grade('rc_sixth_form') }}::integer           as rc_sixth_form,

        -- Start date of the latest FULL inspection (the report-card
        -- inspection in the renewed framework). Guarded in the final select:
        -- only kept when the row actually carries report-card grades, because
        -- in legacy-format files this column is the legacy inspection date.
        to_date(nullif(trim(rc_inspection_date), 'NULL'), 'DD/MM/YYYY')   as rc_inspection_date_raw,

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

select
    *,
    case
        when rc_safeguarding_met is not null
          or rc_inclusion is not null
          or rc_curriculum_teaching is not null
          or rc_achievement is not null
          or rc_attendance_behaviour is not null
          or rc_personal_development is not null
          or rc_leadership_governance is not null
        then rc_inspection_date_raw
    end as rc_inspection_date
from renamed
where inspection_date is not null
