from decimal import Decimal

from app.algorithem import ApplicantExpenseProfileInput, evaluate_applicant_expense_profile


def test_applicant_profile_recommends_review_and_days_cap() -> None:
    result = evaluate_applicant_expense_profile(
        ApplicantExpenseProfileInput(
            applicant_claim_count_90d=6,
            peer_claim_count_p75_90d=4,
            applicant_amount_90d=Decimal("42800"),
            available_peer_budget_90d=Decimal("150000"),
            amount_percentile=Decimal("88"),
            peer_amount_median_90d=Decimal("25000"),
            adjusted_or_returned_count_180d=3,
            approved_claim_count_180d=12,
            requested_days=Decimal("5"),
            peer_travel_days_p75=Decimal("3"),
            business_buffer_days=Decimal("1"),
            claim_amount=Decimal("8000"),
            peer_daily_cost_baseline=Decimal("1400"),
            tolerance_factor=Decimal("1.20"),
        )
    )

    assert result.profile_score == 68
    assert result.profile_level == "review"
    assert result.recommendation_level == "review"
    assert result.travel_days_ratio == Decimal("1.6667")
    assert result.suggested_days == Decimal("4.0000")
    assert result.suggested_amount_cap == Decimal("6720.00")
    assert "applicant.amount_percentile.p85" in result.basis_codes
    assert "travel.days_ratio.review" in result.basis_codes


def test_applicant_profile_handles_missing_baselines_without_false_risk() -> None:
    result = evaluate_applicant_expense_profile(
        ApplicantExpenseProfileInput(
            applicant_claim_count_90d=1,
            applicant_amount_90d=Decimal("800"),
            requested_days=Decimal("2"),
            claim_amount=Decimal("800"),
        )
    )

    assert result.profile_score == 0
    assert result.profile_level == "normal"
    assert result.recommendation_level == "normal"
    assert result.frequency_ratio == Decimal("0")
    assert result.daily_cost_ratio == Decimal("0")
    assert result.suggested_days == Decimal("2.0000")
    assert result.suggested_amount_cap is None
    assert result.basis_codes == []


def test_entertainment_deviation_can_escalate_recommendation() -> None:
    result = evaluate_applicant_expense_profile(
        ApplicantExpenseProfileInput(
            entertainment_amount=Decimal("3000"),
            attendee_count=3,
            entertainment_standard_cap=Decimal("600"),
            same_customer_frequency_90d=3,
            applicant_entertainment_percentile=Decimal("96"),
        )
    )

    assert result.entertainment_deviation_score == 100
    assert result.current_claim_deviation_score == 100
    assert result.profile_score == 15
    assert result.profile_level == "normal"
    assert result.recommendation_level == "escalation"
    assert "entertainment.per_capita.escalation" in result.basis_codes
    assert "entertainment.same_customer.frequency_review" in result.basis_codes
    assert "entertainment.amount_percentile.p95" in result.basis_codes
