-- IMPORT
WITH frozen_account_incidents AS (
    SELECT * FROM {{ ref('fct_exchange__frozen_account_incidents') }}
),

orders AS (
    SELECT * FROM {{ ref('fct_exchange__transaction_orders') }}
),

costs AS (
    SELECT * FROM {{ ref('fct_exchange__transaction_costs') }}
),

companies AS (
    SELECT * FROM {{ ref('dim_exchange__companies') }}
),


-- LOGIC
affected_companies_order_history AS (

    SELECT
        *

    FROM 
        orders,
        LATERAL 
            (
            SELECT 
                frozen_account_case_id,
                ticket_created_datetime,
                ticket_updated_datetime,
                ticket_closed_datetime
            FROM 
                frozen_account_incidents 
            WHERE  
                orders.company_id = frozen_account_incidents.company_id
            )

    WHERE transaction_status IN ('UPLOADED_BANK_SLIP')

),

order_associated_with_each_incident AS (
-- Some booking_marking_id may have 2 or more orders
-- This CTE select the order closest to the incident_ticket_created_datetime as the affected order
    SELECT
        frozen_account_incidents.* EXCLUDE _dbt_ran_datetime,

        orders.order_id,
        orders.base_currency_id,
        orders.quote_currency_id,
        orders.transaction_status,
        orders.order_base_value,
        orders.order_base_service_charge,
        orders.order_base_tax,
        orders.order_quote_value,
        orders.total_order_value_rm,
        orders.order_base_to_quote_currency_exchange_rate,
        orders.order_created_datetime,
        orders.customer_made_booking_datetime,
        orders.customer_placed_order_datetime,
        orders.customer_made_payment_datetime,
        orders.account_verified_payment_datetime,
        orders.first_white_form_generated_datetime,
        orders.last_white_form_generated_datetime,
        orders.operation_uploaded_bank_slip_datetime,
        orders.next_order_created_datetime,

        ABS(DATEDIFF(minute, ticket_created_datetime, operation_uploaded_bank_slip_datetime))   AS time_diff,

        ROW_NUMBER() OVER (PARTITION BY frozen_account_case_id ORDER BY time_diff)              AS row_num_idx

    FROM frozen_account_incidents

    LEFT JOIN orders
        ON (frozen_account_incidents.booking_marking_id = orders.booking_marking_id)

    QUALIFY
        row_num_idx = 1

),

aggregated_metrics AS (

    SELECT
        frozen_account_case_id,
        SUM(total_order_value_rm)       AS total_lifetime_value_rm,

        SUM(CASE
                WHEN 
                    ticket_created_datetime >= customer_placed_order_datetime
                    AND 
                    transaction_status = 'UPLOADED_BANK_SLIP'
                THEN total_order_value_rm
                ELSE 0
            END)        AS before_frozen_lifetime_value_rm,

        SUM(CASE 
                WHEN 
                    customer_placed_order_datetime > ticket_created_datetime
                    AND 
                    transaction_status = 'UPLOADED_BANK_SLIP'
                THEN total_order_value_rm
                ELSE 0
            END)        AS after_unfreeze_lifetime_value_rm,
        
        MIN(CASE
                WHEN 
                    customer_made_booking_datetime > ticket_created_datetime
                    AND booking_status = 'COMPLETED'
                THEN customer_made_booking_datetime
            END)        AS after_unfreeze_first_booking_datetime,

        MIN(CASE
                WHEN customer_placed_order_datetime > ticket_created_datetime
                    AND order_status = 'COMPLETED'
                THEN customer_placed_order_datetime
            END)        AS after_unfreeze_first_order_datetime,
        
        SUM(CASE 
                WHEN ticket_created_datetime > customer_placed_order_datetime
                    AND transaction_status = 'UPLOADED_BANK_SLIP'
                THEN 1
                ELSE 0
            END)        AS count_order_before_frozen,

        SUM(CASE
                WHEN customer_placed_order_datetime > ticket_created_datetime
                    AND transaction_status = 'UPLOADED_BANK_SLIP'
                THEN 1
                ELSE 0
            END)        AS count_order_after_unfreeze,

        MAX(customer_placed_order_datetime)                                             AS latest_order_placed_datetime,
        ROUND(DIV0(before_frozen_lifetime_value_rm, count_order_before_frozen), 2)      AS before_frozen_average_lifetime_value_rm,
        ROUND(DIV0(after_unfreeze_lifetime_value_rm,count_order_after_unfreeze), 2)     AS after_unfreeze_average_lifetime_value_rm

    FROM affected_companies_order_history

    GROUP BY frozen_account_case_id

),

history_and_metric_join AS (

    SELECT
        order_associated_with_each_incident.*,

        costs.supplier_company_id,

        companies.company_marking_id,
        companies.company_type,
        companies.business_type,
        companies.company_created_datetime                                                  AS company_register_datetime,

        aggregated_metrics.total_lifetime_value_rm,
        aggregated_metrics.before_frozen_lifetime_value_rm,
        aggregated_metrics.after_unfreeze_lifetime_value_rm,
        aggregated_metrics.before_frozen_average_lifetime_value_rm,
        aggregated_metrics.after_unfreeze_average_lifetime_value_rm,
        aggregated_metrics.after_unfreeze_first_booking_datetime,
        aggregated_metrics.after_unfreeze_first_order_datetime,
        aggregated_metrics.count_order_before_frozen,
        aggregated_metrics.count_order_after_unfreeze,
        aggregated_metrics.latest_order_placed_datetime,

        COUNT(DISTINCT order_id) 
            OVER 
            (PARTITION BY order_associated_with_each_incident.company_id)                   AS count_company_frozen_cases,

        '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}'   AS _dbt_ran_datetime

    FROM order_associated_with_each_incident

    LEFT JOIN aggregated_metrics
        ON (order_associated_with_each_incident.frozen_account_case_id = aggregated_metrics.frozen_account_case_id)

    LEFT JOIN companies
        ON (order_associated_with_each_incident.company_id = companies.company_id)

    LEFT JOIN costs
        ON (order_associated_with_each_incident.order_id = costs.transaction_order_id)

),


-- FINAL
final_rep_exchange__frozen_accounts AS (

    SELECT
        -- id
        frozen_account_case_id,
        crm_ticket_id,
        booking_id,
        booking_marking_id,
        company_id,
        company_marking_id,
        order_id,
        base_currency_id,
        quote_currency_id,
        supplier_company_id,
        recipient_bank_id,
        recipient_bank_country_id,

        -- dimensions
        transaction_status,
        account_freeze_reason,
        incident_ticket_status,
        agent_reply_status,
        company_type,
        business_type,
        service_type,        
        recipient_bank_type,
        recipient_bank_name,
        recipient_bank_branch,
        affected_order_currency,

        -- measures
        count_company_frozen_cases,
        affected_order_amount,
        order_base_to_quote_currency_exchange_rate,
        order_base_value,
        order_base_tax,
        order_base_service_charge,
        order_quote_value,
        total_order_value_rm,
        total_lifetime_value_rm,
        before_frozen_lifetime_value_rm,
        after_unfreeze_lifetime_value_rm,
        count_order_before_frozen,
        count_order_after_unfreeze,
        before_frozen_average_lifetime_value_rm,
        after_unfreeze_average_lifetime_value_rm,

        -- date/time
        company_register_datetime,
        ticket_created_datetime,
        ticket_updated_datetime,
        ticket_closed_datetime,
        after_unfreeze_first_booking_datetime,
        after_unfreeze_first_order_datetime,
        latest_order_placed_datetime,
        order_created_datetime,
        customer_made_booking_datetime,
        customer_placed_order_datetime,
        customer_made_payment_datetime,
        account_verified_payment_datetime,
        first_white_form_generated_datetime,
        last_white_form_generated_datetime,
        operation_uploaded_bank_slip_datetime,
        next_order_created_datetime,

        -- metadata
        _dbt_ran_datetime

    FROM history_and_metric_join

)

SELECT * FROM final_rep_exchange__frozen_accounts