"""Bank transaction status lifecycle and matching confidence bands."""

from __future__ import annotations

from datetime import datetime, timezone

# Status strings aligned with the frontend BankStatus type.
STATUS_UNMATCHED = "Unmatched"
STATUS_SUGGESTED = "Suggested"
STATUS_MATCHED = "Matched"
STATUS_PARTIAL = "Partial"
STATUS_NEEDS_REVIEW = "Needs review"
STATUS_IGNORED = "Ignored"

ALL_STATUSES = frozenset(
    {
        STATUS_UNMATCHED,
        STATUS_SUGGESTED,
        STATUS_MATCHED,
        STATUS_PARTIAL,
        STATUS_NEEDS_REVIEW,
        STATUS_IGNORED,
    }
)

# Minimum confidence (0–100) to surface a suggestion automatically.
SUGGEST_THRESHOLD = 60
HIGH_CONFIDENCE_THRESHOLD = 85


class BankWorkflowError(ValueError):
    pass


def utc_now_iso() -> str:
    return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")


def confidence_label(score: float) -> str:
    if score >= HIGH_CONFIDENCE_THRESHOLD:
        return "High"
    if score >= SUGGEST_THRESHOLD:
        return "Medium"
    if score > 0:
        return "Low"
    return "None"


def status_from_confidence(score: float) -> str:
    if score >= SUGGEST_THRESHOLD:
        return STATUS_SUGGESTED
    if score >= 35:
        return STATUS_NEEDS_REVIEW
    return STATUS_UNMATCHED
