with
    int_flagging_booking_categorisation as (
        select * from {{ ref("int_flagging_booking_categorisation") }}
    )
select
    'RISK_VS_CLAIM' as flagging_analysis_type,
    completed_bookings as count_total,
    -- TP 
    completed_risk_with_claim_bookings as count_true_positive,
    -- TN
    completed_no_risk_without_claim_bookings as count_true_negative,
    -- FP
    completed_risk_without_claim_bookings as count_false_positive,
    -- FN
    completed_no_risk_with_claim_bookings as count_false_negative,
    -- TP %
    cast(completed_risk_with_claim_bookings as decimal)
    / completed_bookings as true_positive_score,
    -- TN %
    cast(completed_no_risk_without_claim_bookings as decimal)
    / completed_bookings as true_negative_score,
    -- FP %
    cast(completed_risk_without_claim_bookings as decimal)
    / completed_bookings as false_positive_score,
    -- FN %
    cast(completed_no_risk_with_claim_bookings as decimal)
    / completed_bookings as false_negative_score,
    -- RECALL: TP / (TP + FN)
    cast(completed_risk_with_claim_bookings as decimal) / (
        completed_risk_with_claim_bookings + completed_no_risk_with_claim_bookings
    ) as recall_score,
    -- PRECISION: TP / (TP + FP)
    cast(completed_risk_with_claim_bookings as decimal) / (
        completed_risk_with_claim_bookings + completed_risk_without_claim_bookings
    ) as precision_score,
    -- FALSE POSITIVE RATE: FP / (FP + TN)
    cast(completed_risk_without_claim_bookings as decimal) / (
        completed_risk_without_claim_bookings + completed_no_risk_without_claim_bookings
    ) as false_positive_rate_score,
    -- F1 SCORE: 2*TP / (2*TP + FN + FP)
    cast(2 * completed_risk_with_claim_bookings as decimal) / (
        2 * completed_risk_with_claim_bookings
        + completed_no_risk_with_claim_bookings
        + completed_risk_without_claim_bookings
    ) as f1_score,
    -- F2 SCORE: 5*TP / (5*TP + 4*FN + FP)
    cast(5 * completed_risk_with_claim_bookings as decimal) / (
        5 * completed_risk_with_claim_bookings
        + 4 * completed_no_risk_with_claim_bookings
        + completed_risk_without_claim_bookings
    ) as f2_score
from int_flagging_booking_categorisation

union all

select
    'RISK_VS_SUBMITTED_PAYOUT' as flagging_analysis_type,
    completed_not_awaiting_resolution_bookings as count_total,
    -- TP 
    completed_risk_with_submitted_payout_bookings as count_true_positive,
    -- TN
    completed_no_risk_without_submitted_payout_bookings as count_true_negative,
    -- FP
    completed_risk_without_submitted_payout_bookings as count_false_positive,
    -- FN
    completed_no_risk_with_submitted_payout_bookings as count_false_negative,
    -- TP %
    cast(completed_risk_with_submitted_payout_bookings as decimal)
    / completed_not_awaiting_resolution_bookings as true_positive_score,
    -- TN %
    cast(completed_no_risk_without_submitted_payout_bookings as decimal)
    / completed_not_awaiting_resolution_bookings as true_negative_score,
    -- FP %
    cast(completed_risk_without_submitted_payout_bookings as decimal)
    / completed_not_awaiting_resolution_bookings as false_positive_score,
    -- FN %
    cast(completed_no_risk_with_submitted_payout_bookings as decimal)
    / completed_not_awaiting_resolution_bookings as false_negative_score,
    -- RECALL: TP / (TP + FN)
    cast(completed_risk_with_submitted_payout_bookings as decimal) / (
        completed_risk_with_submitted_payout_bookings
        + completed_no_risk_with_submitted_payout_bookings
    ) as recall_score,
    -- PRECISION: TP / (TP + FP)
    cast(completed_risk_with_submitted_payout_bookings as decimal) / (
        completed_risk_with_submitted_payout_bookings
        + completed_risk_without_submitted_payout_bookings
    ) as precision_score,
    -- FALSE POSITIVE RATE: FP / (FP + TN)
    cast(completed_risk_without_submitted_payout_bookings as decimal) / (
        completed_risk_without_submitted_payout_bookings
        + completed_no_risk_without_submitted_payout_bookings
    ) as false_positive_rate_score,
    -- F1 SCORE: 2*TP / (2*TP + FN + FP)
    cast(2 * completed_risk_with_submitted_payout_bookings as decimal) / (
        2 * completed_risk_with_submitted_payout_bookings
        + completed_no_risk_with_submitted_payout_bookings
        + completed_risk_without_submitted_payout_bookings
    ) as f1_score,
    -- F2 SCORE: 5*TP / (5*TP + 4*FN + FP)
    cast(5 * completed_risk_with_submitted_payout_bookings as decimal) / (
        5 * completed_risk_with_submitted_payout_bookings
        + 4 * completed_no_risk_with_submitted_payout_bookings
        + completed_risk_without_submitted_payout_bookings
    ) as f2_score
from int_flagging_booking_categorisation
