from __future__ import annotations

from typing import Protocol

from v1.auth.schemas import (
    PasswordResetConfirmRequest,
    PasswordResetRequest,
    SessionCreateRequest,
    SessionRefreshRequest,
    SessionResponse,
    UserByEmailResponse,
    VerificationCreateRequest,
    VerificationCreateResponse,
    VerificationResendRequest,
    VerificationVerifyRequest,
)


class AuthServiceGateway(Protocol):
    async def create_verification(
        self, request: VerificationCreateRequest
    ) -> VerificationCreateResponse:
        raise NotImplementedError

    async def verify_verification(
        self, request: VerificationVerifyRequest
    ) -> SessionResponse:
        raise NotImplementedError

    async def resend_verification(self, request: VerificationResendRequest) -> None:
        raise NotImplementedError

    async def create_session(self, request: SessionCreateRequest) -> SessionResponse:
        raise NotImplementedError

    async def refresh_session(self, request: SessionRefreshRequest) -> SessionResponse:
        raise NotImplementedError

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

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

    async def request_password_reset(self, request: PasswordResetRequest) -> None:
        raise NotImplementedError

    async def confirm_password_reset(
        self, request: PasswordResetConfirmRequest
    ) -> None:
        raise NotImplementedError


class AuthService:
    _gateway: AuthServiceGateway

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

    async def create_verification(
        self, request: VerificationCreateRequest
    ) -> VerificationCreateResponse:
        return await self._gateway.create_verification(request)

    async def verify_verification(
        self, request: VerificationVerifyRequest
    ) -> SessionResponse:
        return await self._gateway.verify_verification(request)

    async def resend_verification(self, request: VerificationResendRequest) -> None:
        await self._gateway.resend_verification(request)

    async def create_session(self, request: SessionCreateRequest) -> SessionResponse:
        return await self._gateway.create_session(request)

    async def refresh_session(self, request: SessionRefreshRequest) -> SessionResponse:
        return await self._gateway.refresh_session(request)

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

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

    async def request_password_reset(self, request: PasswordResetRequest) -> None:
        await self._gateway.request_password_reset(request)

    async def confirm_password_reset(
        self, request: PasswordResetConfirmRequest
    ) -> None:
        await self._gateway.confirm_password_reset(request)
