"""Driver domain policies — Phase 10.1."""
from __future__ import annotations

from app.models.types import DriverLifecycleAction, DriverStatus
from app.validators.drivers import (
    ensure_driver_lifecycle_transition,
    target_status_for,
    validate_reason,
)


class DriverLifecyclePolicy:
    """Enforces legal driver status transitions and reason rules."""

    REQUIRES_REASON = frozenset(
        {
            DriverLifecycleAction.SUSPEND,
            DriverLifecycleAction.BLOCK,
            DriverLifecycleAction.DEACTIVATE,
            DriverLifecycleAction.ARCHIVE,
        }
    )

    def assert_transition(
        self,
        *,
        action: DriverLifecycleAction,
        current: DriverStatus,
        reason: str | None = None,
    ) -> tuple[DriverStatus, str | None]:
        ensure_driver_lifecycle_transition(action=action, current=current)
        cleaned = validate_reason(
            reason, required=action in self.REQUIRES_REASON
        )
        return target_status_for(action), cleaned
