"""Service units, services and work types (specification 11.1). Hierarchy: service unit → (service) → work type. Rows are never deleted, only deactivated. A deactivated row keeps its children's own ``is_active`` flags; the children are just not selectable while a parent is inactive. """ from dataclasses import dataclass, replace from typing import Self from apps.catalog.domain.exceptions import ServiceNotInUnit from apps.catalog.domain.value_objects import CatalogName @dataclass(frozen=True, slots=True) class ServiceUnit: id: int | None name: CatalogName is_shared: bool = False # "Kaikille yhteiset": its work types are selectable in every unit is_active: bool = True sort_order: int = 0 @classmethod def register(cls, *, name: CatalogName, sort_order: int) -> Self: return cls(id=None, name=name, sort_order=sort_order) @classmethod def reconstitute( cls, *, id: int, name: CatalogName, is_shared: bool, is_active: bool, sort_order: int ) -> Self: return cls(id, name, is_shared, is_active, sort_order) def rename(self, name: CatalogName) -> Self: return replace(self, name=name) def with_active(self, active: bool) -> Self: return replace(self, is_active=active) def with_id(self, id: int) -> Self: return replace(self, id=id) @dataclass(frozen=True, slots=True) class Service: id: int | None service_unit_id: int name: CatalogName is_active: bool = True sort_order: int = 0 @classmethod def register(cls, *, service_unit_id: int, name: CatalogName, sort_order: int) -> Self: return cls(id=None, service_unit_id=service_unit_id, name=name, sort_order=sort_order) @classmethod def reconstitute( cls, *, id: int, service_unit_id: int, name: CatalogName, is_active: bool, sort_order: int ) -> Self: return cls(id, service_unit_id, name, is_active, sort_order) def rename(self, name: CatalogName) -> Self: return replace(self, name=name) def with_active(self, active: bool) -> Self: return replace(self, is_active=active) def with_id(self, id: int) -> Self: return replace(self, id=id) @dataclass(frozen=True, slots=True) class WorkType: """A work type sits directly under its service unit (``service_id`` None) or under one of that unit's services.""" id: int | None service_unit_id: int service_id: int | None name: CatalogName is_active: bool = True sort_order: int = 0 @classmethod def register( cls, *, service_unit_id: int, service: Service | None, name: CatalogName, sort_order: int, ) -> Self: _check_service(service_unit_id, service) return cls( id=None, service_unit_id=service_unit_id, service_id=service.id if service else None, name=name, sort_order=sort_order, ) @classmethod def reconstitute( cls, *, id: int, service_unit_id: int, service_id: int | None, name: CatalogName, is_active: bool, sort_order: int, ) -> Self: return cls(id, service_unit_id, service_id, name, is_active, sort_order) def move_to(self, service: Service | None, *, sort_order: int) -> Self: """Move under another service of the same unit, or directly under the unit.""" _check_service(self.service_unit_id, service) return replace(self, service_id=service.id if service else None, sort_order=sort_order) def rename(self, name: CatalogName) -> Self: return replace(self, name=name) def with_active(self, active: bool) -> Self: return replace(self, is_active=active) def with_id(self, id: int) -> Self: return replace(self, id=id) def _check_service(service_unit_id: int, service: Service | None) -> None: if service is not None and service.service_unit_id != service_unit_id: raise ServiceNotInUnit()