from fastapi import APIRouter, HTTPException, status, Depends
from app.schemas import UserRegister, UserLogin, TokenResponse
from app.services.auth_service import AuthService
from app.auth import get_current_user

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

@router.post("/register", response_model=TokenResponse)
def register(user_data: UserRegister):
    """Register a new user"""
    try:
        return AuthService.register(user_data)
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )

@router.post("/login", response_model=TokenResponse)
def login(user_data: UserLogin):
    """Login user"""
    try:
        return AuthService.login(user_data)
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=str(e)
        )

@router.get("/me")
def get_current_user_info(current_user: dict = Depends(get_current_user)):
    """Get current user info"""
    return {"user_id": current_user["user_id"], "email": current_user["email"]}
