import logging
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel

from app.utils.auth import create_token

logger = logging.getLogger(f'{__name__}.auth')

router = APIRouter()


class TokenRequest(BaseModel):
    """Request model for token creation"""
    user_id: str
    username: str
    discord_id: str


class TokenResponse(BaseModel):
    """Response model for token creation"""
    access_token: str
    token_type: str = "bearer"


@router.post("/token", response_model=TokenResponse)
async def create_auth_token(request: TokenRequest):
    """
    Create JWT token for authenticated user

    TODO Phase 1: Implement Discord OAuth flow
    For now, this is a stub that creates tokens from provided user data
    """
    try:
        user_data = {
            "user_id": request.user_id,
            "username": request.username,
            "discord_id": request.discord_id
        }

        token = create_token(user_data)

        return TokenResponse(access_token=token)

    except Exception as e:
        logger.error(f"Token creation error: {e}")
        raise HTTPException(status_code=500, detail="Failed to create token")


@router.get("/verify")
async def verify_auth():
    """
    Verify authentication status

    TODO Phase 1: Implement full auth verification
    """
    return {"authenticated": True, "message": "Auth verification stub"}
