"""Unsupported repository implementations for memory mode.

Provide repository interfaces that raise NotImplementedError for
operations requiring database persistence.
"""

from __future__ import annotations

from collections.abc import Sequence
from datetime import datetime
from typing import TYPE_CHECKING
from uuid import UUID

from noteflow.config.constants import ERR_SERVER_RESTARTED

_ERR_ANNOTATIONS_DB = "Annotations require database persistence"
_ERR_DIARIZATION_DB = "Diarization jobs require database persistence"
_ERR_PREFERENCES_DB = "Preferences require database persistence"
_ERR_NER_ENTITIES_DB = "NER entities require database persistence"
_ERR_USAGE_EVENTS_DB = "Usage events require database persistence"

if TYPE_CHECKING:
    from noteflow.application.observability.ports import UsageEvent
    from noteflow.domain.entities import Annotation
    from noteflow.domain.entities.named_entity import NamedEntity
    from noteflow.domain.value_objects import AnnotationId, MeetingId
    from noteflow.infrastructure.persistence.repositories import (
        DiarizationJob,
        StreamingTurn,
    )
    from noteflow.infrastructure.persistence.repositories.preferences_repo import (
        PreferenceWithMetadata,
    )


class UnsupportedAnnotationRepository:
    """Annotation repository that raises for unsupported operations.

    Use in memory mode where annotations require database persistence.
    """

    async def add(self, annotation: Annotation) -> Annotation:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)

    async def get(self, annotation_id: AnnotationId) -> Annotation | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)

    async def get_by_meeting(self, meeting_id: MeetingId) -> Sequence[Annotation]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)

    async def get_by_time_range(
        self,
        meeting_id: MeetingId,
        start_time: float,
        end_time: float,
    ) -> Sequence[Annotation]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)

    async def update(self, annotation: Annotation) -> Annotation:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)

    async def delete(self, annotation_id: AnnotationId) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_ANNOTATIONS_DB)


class UnsupportedDiarizationJobRepository:
    """Diarization job repository that raises for unsupported operations.

    Use in memory mode where jobs require database persistence.
    """

    async def create(self, job: DiarizationJob) -> DiarizationJob:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def get(self, job_id: str) -> DiarizationJob | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def update_status(
        self,
        job_id: str,
        status: int,
        *,
        segments_updated: int | None = None,
        speaker_ids: list[str] | None = None,
        error_message: str | None = None,
        started_at: datetime | None = None,
    ) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def get_active_for_meeting(self, meeting_id: str) -> DiarizationJob | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def get_all_active(self) -> list[DiarizationJob]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def prune_completed(self, ttl_seconds: float) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def add_streaming_turns(
        self,
        meeting_id: str,
        turns: Sequence[StreamingTurn],
    ) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def get_streaming_turns(self, meeting_id: str) -> list[StreamingTurn]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def clear_streaming_turns(self, meeting_id: str) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)

    async def mark_running_as_failed(
        self,
        error_message: str = ERR_SERVER_RESTARTED,
    ) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_DIARIZATION_DB)


class UnsupportedPreferencesRepository:
    """Preferences repository that raises for unsupported operations.

    Use in memory mode where preferences require database persistence.
    """

    async def get(self, key: str) -> object | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)

    async def get_bool(self, key: str, default: bool = False) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)

    async def set(self, key: str, value: object) -> None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)

    async def delete(self, key: str) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)

    async def get_all_with_metadata(
        self,
        keys: Sequence[str] | None = None,
    ) -> list[PreferenceWithMetadata]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)

    async def set_bulk(self, preferences: dict[str, object]) -> None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_PREFERENCES_DB)


class UnsupportedEntityRepository:
    """Entity repository that raises for unsupported operations.

    Use in memory mode where NER entities require database persistence.
    """

    async def save(self, entity: NamedEntity) -> NamedEntity:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def save_batch(self, entities: Sequence[NamedEntity]) -> Sequence[NamedEntity]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def get(self, entity_id: UUID) -> NamedEntity | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def get_by_meeting(self, meeting_id: MeetingId) -> Sequence[NamedEntity]:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def delete_by_meeting(self, meeting_id: MeetingId) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def update_pinned(self, entity_id: UUID, is_pinned: bool) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def exists_for_meeting(self, meeting_id: MeetingId) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def update(
        self,
        entity_id: UUID,
        text: str | None = None,
        category: str | None = None,
    ) -> NamedEntity | None:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)

    async def delete(self, entity_id: UUID) -> bool:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_NER_ENTITIES_DB)


class UnsupportedUsageEventRepository:
    """Usage event repository that raises for unsupported operations.

    Use in memory mode where usage events require database persistence.
    """

    async def add(self, event: UsageEvent) -> UsageEvent:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_USAGE_EVENTS_DB)

    async def add_batch(self, events: Sequence[UsageEvent]) -> int:
        """Not supported in memory mode."""
        raise NotImplementedError(_ERR_USAGE_EVENTS_DB)
