"""Dispatch domain policies — Phase 10.5."""
from __future__ import annotations

from app.models.types import DispatchJobAction, DispatchJobStatus
from app.validators.dispatch import (
    ensure_dispatch_job_transition,
    target_status_for,
    validate_dispatch_reason,
)


class DispatchJobPolicy:
    REQUIRES_REASON = frozenset(
        {DispatchJobAction.CANCEL, DispatchJobAction.FAIL}
    )

    def assert_transition(
        self,
        *,
        action: DispatchJobAction,
        current: DispatchJobStatus,
        reason: str | None = None,
    ) -> tuple[DispatchJobStatus, str | None]:
        ensure_dispatch_job_transition(action=action, current=current)
        cleaned = validate_dispatch_reason(
            reason, required=action in self.REQUIRES_REASON
        )
        return target_status_for(action), cleaned
