with
    stg_xero__contacts as (select * from {{ ref("stg_xero__contacts") }}),
    contact_with_deal as (
        select
            id_contact,
            case
                when account_number ~ '^[0-9]{10,11}$' then account_number else null
            end::text as id_deal,
            updated_at_utc
        from stg_xero__contacts
    ),
    deals_ranked_per_updated_at_utc as (
        select
            id_contact,
            id_deal,
            row_number() over (
                partition by id_deal order by updated_at_utc desc
            ) as deal_rank_per_updated_at_utc
        from contact_with_deal
        -- Ensuring id deal is set 
        where id_deal is not null
    )
select
    c.id_contact,
    c.account_number,
    cwd.id_deal,
    case
        when cwd.id_deal is null then true else false
    end as is_contact_missing_id_deal,
    -- The following ensures is the last updated contact that actually contains a deal
    case
        when dr.deal_rank_per_updated_at_utc = 1 then true else false
    end as is_latest_updated_contact_per_deal,
    c.contact_name,
    c.is_customer,
    c.is_supplier,
    c.tax_number,
    c.contact_first_name,
    c.contact_last_name,
    c.phones,
    c.website,
    c.balances,
    c.discount,
    c.addresses,
    c.attachments,
    c.email_addresses,
    c.payment_terms,
    c.batch_payments,
    c.branding_theme,
    c.contact_groups,
    c.contact_number,
    c.contact_status,
    c.skyper_user_name,
    c.contact_persons,
    c.has_attachments,
    c.updated_at_utc,
    c.updated_date_utc,
    c.xero_network_key,
    c.default_currency_iso_4217,
    c.validation_errors,
    c.bank_account_details,
    c.has_validation_errors,
    c.tracking_category_name,
    c.account_payable_tax_type,
    c.tracking_category_option,
    c.sales_default_account_code,
    c.sales_tracking_categories,
    c.account_receivable_tax_type,
    c.purchases_default_account_code,
    c.purchases_tracking_categories,
    c.dwh_extracted_at_utc
from stg_xero__contacts c
left join contact_with_deal cwd on c.id_contact = cwd.id_contact
left join deals_ranked_per_updated_at_utc dr on c.id_contact = dr.id_contact
