"""Authentication API endpoints."""

from __future__ import annotations

from typing import TYPE_CHECKING

from vikdata.models.auth import (
    AccessTokenResponse,
    LoginRequest,
    LogoutResponse,
    RefreshRequest,
    TokenResponse,
    UserResponse,
)

if TYPE_CHECKING:
    from vikdata.client import VikdataClient


class AuthAPI:
    """Authentication API endpoints.

    Handles login, token refresh, logout, and current user info.
    """

    def __init__(self, client: "VikdataClient") -> None:
        """Initialize auth API.

        Args:
            client: VikdataClient instance
        """
        self._client = client

    async def login(self, email: str, password: str) -> TokenResponse:
        """Authenticate user with email and password.

        Args:
            email: User email address
            password: User password

        Returns:
            TokenResponse with access and refresh tokens

        Raises:
            AuthenticationError: Invalid credentials
            ValidationError: Invalid input data
        """
        request = LoginRequest(email=email, password=password)
        response = await self._client.request(
            "POST",
            "/api/v1/auth/login",
            json=request,
            response_model=TokenResponse,
        )

        # Automatically set the access token on the client
        if response:
            self._client.set_access_token(response.access_token)

        return response

    async def refresh(self, refresh_token: str) -> AccessTokenResponse:
        """Refresh an access token using a valid refresh token.

        Args:
            refresh_token: JWT refresh token

        Returns:
            AccessTokenResponse with new access token

        Raises:
            AuthenticationError: Invalid or expired refresh token
        """
        request = RefreshRequest(refresh_token=refresh_token)
        response = await self._client.request(
            "POST",
            "/api/v1/auth/refresh",
            json=request,
            response_model=AccessTokenResponse,
        )

        # Automatically update the access token on the client
        if response:
            self._client.set_access_token(response.access_token)

        return response

    async def logout(self) -> LogoutResponse:
        """Logout by blacklisting the current access token.

        Returns:
            LogoutResponse confirmation

        Raises:
            AuthenticationError: No valid access token
        """
        response = await self._client.request(
            "POST",
            "/api/v1/auth/logout",
            response_model=LogoutResponse,
        )

        # Clear the access token from the client
        self._client.clear_access_token()

        return response

    async def me(self) -> UserResponse:
        """Get information about the currently authenticated user.

        Returns:
            UserResponse with user details

        Raises:
            AuthenticationError: No valid access token
        """
        return await self._client.request(
            "GET",
            "/api/v1/auth/me",
            response_model=UserResponse,
        )
