{% set min_date = "2022-01-01" %}
{% set dimensions = ("global", "by_billing_country") %}
-- "by_number_of_listings" excluded on purpose - there's differences because of daily
-- segmentation
with
    new_mtd_host_resolutions as (
        select
            end_date as date,
            dimension,
            dimension_value,
            xero_host_resolution_amount_paid_in_gbp,
            xero_host_resolution_payment_count
        from {{ ref("int_kpis__agg_mtd_host_resolutions") }}
        where
            end_date >= '{{ min_date }}'
            and dimension in {{ dimensions }}
            and dimension_value <> 'UNSET'
    ),
    new_monthly_host_resolutions as (
        select
            end_date as date,
            dimension,
            dimension_value,
            xero_host_resolution_amount_paid_in_gbp,
            xero_host_resolution_payment_count
        from {{ ref("int_kpis__agg_monthly_host_resolutions") }}
        where
            end_date >= '{{ min_date }}'
            and dimension in {{ dimensions }}
            and dimension_value <> 'UNSET'
    ),
    new_host_resolutions as (
        select *
        from new_mtd_host_resolutions
        union all
        select *
        from new_monthly_host_resolutions
    ),
    old_host_resolutions as (
        select
            date,
            dimension,
            dimension_value,
            xero_host_resolution_amount_paid_in_gbp,
            xero_host_resolution_payment_count
        from {{ ref("int_xero__mtd_invoicing_metrics") }}
        where date >= '{{ min_date }}' and dimension in {{ dimensions }}
    ),
    comparison as (
        select
            coalesce(o.date, n.date) as date,
            coalesce(o.dimension, n.dimension) as dimension,
            coalesce(o.dimension_value, n.dimension_value) as dimension_value,
            o.xero_host_resolution_amount_paid_in_gbp
            as old_xero_host_resolution_amount_paid_in_gbp,
            n.xero_host_resolution_amount_paid_in_gbp
            as new_xero_host_resolution_amount_paid_in_gbp,
            coalesce(o.xero_host_resolution_amount_paid_in_gbp, 0) - coalesce(
                n.xero_host_resolution_amount_paid_in_gbp, 0
            ) as diff_xero_host_resolution_amount_paid_in_gbp,
            o.xero_host_resolution_payment_count
            as old_xero_host_resolution_payment_count,
            n.xero_host_resolution_payment_count
            as new_xero_host_resolution_payment_count,
            coalesce(o.xero_host_resolution_payment_count, 0) - coalesce(
                n.xero_host_resolution_payment_count, 0
            ) as diff_xero_host_resolution_payment_count
        from old_host_resolutions o
        full outer join
            new_host_resolutions n
            on o.date = n.date
            and o.dimension = n.dimension
            and o.dimension_value = n.dimension_value
    )
select *
from comparison
where
    diff_xero_host_resolution_amount_paid_in_gbp <> 0
    or diff_xero_host_resolution_payment_count <> 0
order by date desc
