from __future__ import annotations

from decimal import Decimal

from app.algorithem.risk_graph import RiskGraphClaimItemSnapshot, RiskGraphClaimSnapshot
from app.algorithem.risk_graph.profile_baselines import ProfileBaselineUpdater


def test_profile_baseline_updater_outputs_core_dimensions() -> None:
    snapshot = ProfileBaselineUpdater().build_from_claims(
        [
            _claim(
                "c1",
                employee_id="e1",
                department_id="d1",
                expense_type="travel",
                amount="600",
                supplier_id="s-hotel",
                supplier_name="Hotel A",
            ),
            _claim(
                "c2",
                employee_id="e1",
                department_id="d1",
                expense_type="travel",
                amount="900",
                supplier_id="s-hotel",
                supplier_name="Hotel A",
            ),
            _claim(
                "c3",
                employee_id="e2",
                department_id="d2",
                expense_type="meal",
                amount="300",
                supplier_id="s-meal",
                supplier_name="Meal B",
            ),
        ]
    )

    assert snapshot.dimension_counts == {
        "employee": 2,
        "department": 2,
        "supplier": 2,
        "expense_type": 2,
    }
    hotel = next(bucket for bucket in snapshot.buckets if bucket.key == "s-hotel")
    assert hotel.dimension == "supplier"
    assert hotel.sample_size == 2
    assert hotel.claim_count == 2
    assert hotel.total_amount == Decimal("1500")
    assert hotel.p75_amount == Decimal("825")
    assert hotel.as_dict()["total_amount"] == "1500"


def _claim(
    claim_id: str,
    *,
    employee_id: str,
    department_id: str,
    expense_type: str,
    amount: str,
    supplier_id: str,
    supplier_name: str,
) -> RiskGraphClaimSnapshot:
    return RiskGraphClaimSnapshot(
        claim_id=claim_id,
        claim_no=f"BX-{claim_id}",
        employee_id=employee_id,
        employee_name=employee_id,
        department_id=department_id,
        department_name=department_id,
        expense_type=expense_type,
        amount=Decimal(amount),
        items=[
            RiskGraphClaimItemSnapshot(
                item_id=f"item-{claim_id}",
                item_type=expense_type,
                item_amount=Decimal(amount),
                metadata={
                    "supplier_id": supplier_id,
                    "supplier_name": supplier_name,
                },
            )
        ],
    )
