-- Staging model: GIAS establishments
-- Light cleaning, type casting, column renaming from raw GIAS bulk CSV

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

renamed as (
    select
        cast("URN" as integer)                          as urn,
        cast(nullif("LA (code)", '') as integer)        as local_authority_code,
        "LA (name)"                                     as local_authority_name,
        cast(nullif("EstablishmentNumber", '') as integer) as establishment_number,
        "EstablishmentName"                             as school_name,
        cast(nullif(trim("TypeOfEstablishment (code)"), '') as integer)  as school_type_code,
        cast(nullif(trim("PhaseOfEducation (code)"), '') as integer)     as phase_code,
        cast(nullif(trim("OfficialSixthForm (code)"), '') as integer)    as official_sixth_form_code,
        "Gender (name)"                                 as gender,
        cast(nullif(trim("ReligiousCharacter (code)"), '') as integer)   as religious_character_code,
        cast(nullif(trim("AdmissionsPolicy (code)"), '') as integer)     as admissions_policy_code,
        "SchoolCapacity"                                as capacity,
        cast(nullif("NumberOfPupils", '') as integer)   as total_pupils,
        "HeadTitle (name)"                              as head_title,
        "HeadFirstName"                                 as head_first_name,
        "HeadLastName"                                  as head_last_name,
        "TelephoneNum"                                  as telephone,
        "SchoolWebsite"                                 as website,
        "Street"                                        as address_line1,
        "Locality"                                      as address_line2,
        "Town"                                          as town,
        "County (name)"                                 as county,
        "Postcode"                                      as postcode,
        cast(nullif(trim("EstablishmentStatus (code)"), '') as integer)  as status_code,
        case when "OpenDate" = '' then null else to_date("OpenDate", 'DD-MM-YYYY') end   as open_date,
        case when "CloseDate" = '' then null else to_date("CloseDate", 'DD-MM-YYYY') end as close_date,
        "Trusts (name)"                                 as academy_trust_name,
        cast(nullif("Trusts (code)", '') as integer)    as academy_trust_uid,
        "UrbanRural (name)"                             as urban_rural,
        "ParliamentaryConstituency (name)"              as parliamentary_constituency,
        "NurseryProvision (name)"                       as nursery_provision,
        cast(nullif("Easting", '') as integer)          as easting,
        cast(nullif("Northing", '') as integer)         as northing,
        cast(nullif("StatutoryLowAge", '') as integer)  as statutory_low_age,
        cast(nullif("StatutoryHighAge", '') as integer) as statutory_high_age
    from source
    where "URN" is not null
)

select * from renamed
