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

import uuid

from hotpocket_backend.apps.accounts.services import (
    AccountsService as BackendAccountsService,
)
from hotpocket_soa.dto.accounts import AccountOut

from .base import ProxyService, SOAError


class AccountsService(ProxyService):
    class AccountsServiceError(SOAError):
        pass

    class AccountNotFound(AccountsServiceError):
        pass

    def __init__(self):
        super().__init__()
        self.backend_accounts_service = BackendAccountsService()

    def wrap_exception(self, exception: Exception) -> Exception:
        new_exception_args = []
        if len(exception.args) > 0:
            new_exception_args = [exception.args[0]]

        return self.AccountsServiceError(*new_exception_args)

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

            return result
        except SOAError as exception:
            if isinstance(exception.__cause__, BackendAccountsService.AccountNotFound) is True:
                raise self.AccountNotFound(*exception.args) from exception
            else:
                raise
