"""Coordinator-owned weekly reminder and storage authority."""

from __future__ import annotations

from dataclasses import dataclass, replace
from pathlib import Path

from checkin_cli.customer_coaching import CustomerRegistry

from .nutrition_weekly_owner_storage import (
    WeeklyOwnerStorageAuthority,
    bind_weekly_owner_storage,
)
from .nutrition_weekly_reminder_authority import (
    WeeklyReminderAuthorityOwner,
    WeeklyReminderOwnerError,
)
from .nutrition_weekly_reminder_owner_factory import (
    WeeklyReminderProductionInput,
    build_weekly_reminder_authority_owner,
)


@dataclass(frozen=True, slots=True)
class WeeklyHostAuthority:
    """Own weekly capabilities created at the coordinator boundary."""

    reminder: WeeklyReminderAuthorityOwner | None
    owner_storage: WeeklyOwnerStorageAuthority | None = None

    @classmethod
    def from_input(
        cls, reminder_input: WeeklyReminderProductionInput | None
    ) -> WeeklyHostAuthority:
        return cls(
            None
            if reminder_input is None
            else build_weekly_reminder_authority_owner(reminder_input)
        )

    def with_storage(self, owner_actions: Path) -> WeeklyHostAuthority:
        return replace(
            self, owner_storage=bind_weekly_owner_storage(owner_actions)
        )

    def reminder_owner(
        self, registry: CustomerRegistry
    ) -> WeeklyReminderAuthorityOwner:
        """Return the current registry-bound reminder owner."""
        owner = self.reminder
        if owner is None:
            raise WeeklyReminderOwnerError(
                "weekly reminder authority owner is unavailable"
            )
        owner.verify_registry(registry)
        return owner

    def storage(self) -> WeeklyOwnerStorageAuthority:
        """Return the verified weekly owner storage capability."""
        storage = self.owner_storage
        if storage is None:
            raise WeeklyReminderOwnerError("weekly owner storage is unavailable")
        storage.verify()
        return storage
