from app.services.expense_claim_status_registry import (
    claim_status_code,
    normalize_expense_claim_state,
)
from app.services.expense_claim_workflow_constants import (
    APPLICATION_ARCHIVE_STAGE,
    APPLICATION_LINK_STATUS_STAGE,
    ARCHIVE_ACCOUNTING_STAGE,
    FINANCE_APPROVAL_STAGE,
    PAYMENT_PAID_STAGE,
    PAYMENT_PENDING_STAGE,
)


def test_normalize_legacy_finance_review_to_submitted_finance_stage() -> None:
    state = normalize_expense_claim_state(
        "finance_review",
        "finance_review",
        claim_no="SIM-EXP-2026-0001",
        expense_type="travel",
    )

    assert state.status == "submitted"
    assert state.approval_stage == FINANCE_APPROVAL_STAGE
    assert state.status_code == 20
    assert state.changed is True


def test_normalize_reimbursement_archive_stage_differs_from_application_done() -> None:
    reimbursement_state = normalize_expense_claim_state(
        "approved",
        "completed",
        claim_no="SIM-EXP-2026-0002",
        expense_type="travel",
    )
    application_state = normalize_expense_claim_state(
        "approved",
        "completed",
        claim_no="AP-20260602-0001",
        expense_type="travel_application",
    )

    assert reimbursement_state.approval_stage == ARCHIVE_ACCOUNTING_STAGE
    assert application_state.approval_stage == APPLICATION_LINK_STATUS_STAGE


def test_normalize_application_archive_stage_is_distinct_from_approval_done() -> None:
    state = normalize_expense_claim_state(
        "approved",
        APPLICATION_ARCHIVE_STAGE,
        claim_no="AP-20260602-0002",
        expense_type="travel_application",
    )

    assert state.status == "approved"
    assert state.approval_stage == APPLICATION_ARCHIVE_STAGE


def test_normalize_payment_stages_by_status() -> None:
    pending_state = normalize_expense_claim_state("pending_payment", "payment")
    paid_state = normalize_expense_claim_state("paid", "payment")

    assert pending_state.approval_stage == PAYMENT_PENDING_STAGE
    assert paid_state.approval_stage == PAYMENT_PAID_STAGE
    assert claim_status_code("paid") == 50
