# -*- coding: utf-8 -*-
from __future__ import annotations

import uuid

from hotpocket_backend.apps.saves.services import (
    SavesService as BackendSavesService,
)
from hotpocket_soa.dto.saves import SaveIn, SaveOut
from hotpocket_soa.exceptions.backend import Invalid, NotFound

from .base import ProxyService, SOAError


class SavesService(ProxyService):
    class SavesServiceError(SOAError):
        pass

    class NotFound(SavesServiceError):
        pass

    class Invalid(SavesServiceError):
        pass

    def __init__(self):
        super().__init__()
        self.backend_saves_service = BackendSavesService()

    def get_error_class(self) -> type[SOAError]:
        return self.SavesServiceError

    def create(self, *, account_uuid: uuid.UUID, save: SaveIn) -> SaveOut:
        try:
            return SaveOut.model_validate(
                self.call(
                    self.backend_saves_service,
                    'create',
                    account_uuid=account_uuid,
                    save=save,
                ),
                from_attributes=True,
            )
        except Invalid as exception:
            raise self.Invalid(
                exception.CODE.value, exception.message, exception.data,
            )

    def get(self, *, pk: uuid.UUID) -> SaveOut:
        try:
            result = SaveOut.model_validate(
                self.call(
                    self.backend_saves_service,
                    'get',
                    pk=pk,
                ),
                from_attributes=True,
            )

            return result
        except NotFound as exception:
            raise self.NotFound.from_backend_error(exception)
