from typing import Protocol

from zbt.domains.service_request.models import MaterialRecognition, ServiceRequest


class ServiceRequestRepository(Protocol):
    def save_recognition(self, recognition: MaterialRecognition) -> None: ...

    def get_recognition(self, recognition_id: str) -> MaterialRecognition | None: ...

    def save_request(self, request: ServiceRequest) -> None: ...

    def get_request(self, request_id: str) -> ServiceRequest | None: ...

    def list_requests(self, user_id: str | None = None) -> list[ServiceRequest]: ...


class InMemoryServiceRequestRepository:
    def __init__(self) -> None:
        self._recognitions: dict[str, MaterialRecognition] = {}
        self._requests: dict[str, ServiceRequest] = {}

    def save_recognition(self, recognition: MaterialRecognition) -> None:
        self._recognitions[recognition.id] = recognition

    def get_recognition(self, recognition_id: str) -> MaterialRecognition | None:
        return self._recognitions.get(recognition_id)

    def save_request(self, request: ServiceRequest) -> None:
        self._requests[request.id] = request

    def get_request(self, request_id: str) -> ServiceRequest | None:
        return self._requests.get(request_id)

    def list_requests(self, user_id: str | None = None) -> list[ServiceRequest]:
        return [
            request
            for request in self._requests.values()
            if user_id is None or request.user_id == user_id
        ]
