"""Auth routes — /login, /register (only when use_auth=yes)."""

from fastapi import APIRouter

from {{ cookiecutter.project_name }}.schemas.user import Token, UserCreate, UserLogin, UserResponse
from {{ cookiecutter.project_name }}.services.auth_service import AuthService

router = APIRouter()


@router.post("/register", response_model=UserResponse, status_code=201)
async def register(payload: UserCreate) -> UserResponse:
    """Register a new user — returns the public profile."""
    user = await AuthService.register(payload.username, payload.email, payload.password)
    return UserResponse(id=user.id, username=user.username, email=user.email)


@router.post("/login", response_model=Token)
async def login(payload: UserLogin) -> Token:
    """Login with username + password — returns a JWT."""
    access_token = await AuthService.login(payload.username, payload.password)
    return Token(access_token=access_token)