from __future__ import annotations

from typing import Protocol

from v1.auth.schemas import (
    AuthResendCodeResponse,
    AuthSignupStartResponse,
    AuthTokenResponse,
    AuthUserByEmailResponse,
    LoginRequest,
    RefreshRequest,
    SignupResendRequest,
    SignupStartRequest,
    SignupVerifyRequest,
)


class AuthServiceGateway(Protocol):
    async def signup_start(
        self, request: SignupStartRequest
    ) -> AuthSignupStartResponse:
        raise NotImplementedError

    async def signup_verify(self, request: SignupVerifyRequest) -> AuthTokenResponse:
        raise NotImplementedError

    async def signup_resend(
        self, request: SignupResendRequest
    ) -> AuthResendCodeResponse:
        raise NotImplementedError

    async def login(self, request: LoginRequest) -> AuthTokenResponse:
        raise NotImplementedError

    async def refresh(self, request: RefreshRequest) -> AuthTokenResponse:
        raise NotImplementedError

    async def logout(self, refresh_token: str | None) -> None:
        raise NotImplementedError

    async def get_user_by_email(self, email: str) -> AuthUserByEmailResponse:
        raise NotImplementedError


class AuthService:
    _gateway: AuthServiceGateway

    def __init__(self, gateway: AuthServiceGateway) -> None:
        self._gateway = gateway

    async def signup_start(
        self, request: SignupStartRequest
    ) -> AuthSignupStartResponse:
        return await self._gateway.signup_start(request)

    async def signup_verify(self, request: SignupVerifyRequest) -> AuthTokenResponse:
        return await self._gateway.signup_verify(request)

    async def signup_resend(
        self, request: SignupResendRequest
    ) -> AuthResendCodeResponse:
        return await self._gateway.signup_resend(request)

    async def login(self, request: LoginRequest) -> AuthTokenResponse:
        return await self._gateway.login(request)

    async def refresh(self, request: RefreshRequest) -> AuthTokenResponse:
        return await self._gateway.refresh(request)

    async def logout(self, refresh_token: str | None) -> None:
        await self._gateway.logout(refresh_token)

    async def get_user_by_email(self, email: str) -> AuthUserByEmailResponse:
        return await self._gateway.get_user_by_email(email)
