from typing import Annotated

from fastapi import APIRouter, Depends

from app.dependencies import CurrentUsername, get_auth_service
from app.schemas.auth import LoginRequest, RefreshRequest, TokenResponse, UserResponse
from app.services.auth_service import AuthService

router = APIRouter(prefix="/api/auth", tags=["Authentication"])


@router.post("/login", response_model=TokenResponse, summary="Log in and issue JWT tokens")
async def login(
    payload: LoginRequest,
    auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> TokenResponse:
    return await auth_service.authenticate(payload.username, payload.password)


@router.post("/refresh", response_model=TokenResponse, summary="Refresh JWT tokens")
async def refresh(
    payload: RefreshRequest,
    auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> TokenResponse:
    return await auth_service.refresh(payload.refresh_token)


@router.get("/me", response_model=UserResponse, summary="Return current authenticated user")
async def me(
    username: CurrentUsername,
    auth_service: Annotated[AuthService, Depends(get_auth_service)],
) -> UserResponse:
    return await auth_service.get_user(username)
