{% set dimensions = get_kpi_dimensions_per_model("INVOICED_REVENUE") %}

{{
    config(
        materialized="table", unique_key=["end_date", "dimension", "dimension_value"]
    )
}}


{% for dimension in dimensions %}
    select
        -- Unique Key --
        start_date,
        end_date,
        {{ dimension.dimension }} as dimension,
        {{ dimension.dimension_value }} as dimension_value,
        -- Metrics --
        sum(
            xero_basic_protection_net_fees_in_gbp
        ) as xero_basic_protection_net_fees_in_gbp,
        sum(xero_waiver_pro_net_fees_in_gbp) as xero_waiver_pro_net_fees_in_gbp,
        sum(
            xero_id_verification_net_fees_in_gbp
        ) as xero_id_verification_net_fees_in_gbp,
        sum(
            xero_protection_plus_net_fees_in_gbp
        ) as xero_protection_plus_net_fees_in_gbp,
        sum(xero_screening_plus_net_fees_in_gbp) as xero_screening_plus_net_fees_in_gbp,
        sum(
            xero_sex_offenders_check_net_fees_in_gbp
        ) as xero_sex_offenders_check_net_fees_in_gbp,
        sum(xero_protection_pro_net_fees_in_gbp) as xero_protection_pro_net_fees_in_gbp,
        sum(
            xero_basic_screening_net_fees_in_gbp
        ) as xero_basic_screening_net_fees_in_gbp,
        sum(
            xero_old_dashboard_booking_net_fees_in_gbp
        ) as xero_old_dashboard_booking_net_fees_in_gbp,
        sum(xero_booking_net_fees_in_gbp) as xero_booking_net_fees_in_gbp,
        sum(xero_listing_net_fees_in_gbp) as xero_listing_net_fees_in_gbp,
        sum(xero_verification_net_fees_in_gbp) as xero_verification_net_fees_in_gbp,
        sum(xero_operator_net_fees_in_gbp) as xero_operator_net_fees_in_gbp,
        sum(
            xero_waiver_paid_back_to_host_in_gbp
        ) as xero_waiver_paid_back_to_host_in_gbp,
        sum(xero_e_deposit_net_fees_in_gbp) as xero_e_deposit_net_fees_in_gbp,
        sum(xero_athena_net_fees_in_gbp) as xero_athena_net_fees_in_gbp,
        sum(xero_guesty_net_fees_in_gbp) as xero_guesty_net_fees_in_gbp,
        sum(xero_apis_net_fees_in_gbp) as xero_apis_net_fees_in_gbp,
        sum(
            xero_guesty_resolutions_net_fees_in_gbp
        ) as xero_guesty_resolutions_net_fees_in_gbp
    from {{ ref("int_kpis__metric_monthly_invoiced_revenue") }}
    group by 1, 2, 3, 4
    {% if not loop.last %}
        union all
    {% endif %}
{% endfor %}
