{{ config(materialized="table", unique_key="id_deal") }}
with
    hubspot_deals as (
        select id_deal, deal_name as main_deal_name, live_date_utc as deal_start_date
        from {{ ref("int_hubspot__deal") }}
        where live_date_utc is not null
    ),
    core_deals as (
        select
            id_deal,
            main_deal_name,
            first_created_date_utc as deal_start_date,
            main_billing_country_iso_3_per_deal
        from {{ ref("int_core__deal") }}
    )
select
    coalesce(hd.id_deal, cd.id_deal) as id_deal,
    coalesce(hd.main_deal_name, cd.main_deal_name) as main_deal_name,
    cd.main_billing_country_iso_3_per_deal,
    min(
        coalesce(hd.deal_start_date, cd.deal_start_date)
    ) as effective_deal_start_date_utc
from hubspot_deals hd
full outer join core_deals cd on hd.id_deal = cd.id_deal
group by 1, 2, 3
