"""Settlement domain policies — Phase 10.8."""
from __future__ import annotations

from app.models.types import SettlementIntentStatus
from app.validators.dispatch import validate_dispatch_reason
from app.validators.settlement import (
    ensure_settlement_transition,
    target_settlement_status,
)


class SettlementIntentPolicy:
    REQUIRES_REASON = frozenset({"fail", "cancel"})

    def assert_transition(
        self,
        *,
        action: str,
        current: SettlementIntentStatus,
        reason: str | None = None,
    ) -> tuple[SettlementIntentStatus, str | None]:
        ensure_settlement_transition(action=action, current=current)
        cleaned = validate_dispatch_reason(
            reason, required=action in self.REQUIRES_REASON
        )
        return target_settlement_status(action), cleaned
