from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.database import get_db
from app.models import Player
from app.schemas import PlayerStatsResponse

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


@router.get("/{callsign}", response_model=PlayerStatsResponse)
async def get_stats(callsign: str, db: AsyncSession = Depends(get_db)) -> PlayerStatsResponse:
    player = (await db.execute(select(Player).where(Player.callsign == callsign))).scalars().first()
    if player is None:
        raise HTTPException(status_code=404, detail="Player not found")
    return PlayerStatsResponse(callsign=player.callsign, mmr=player.mmr, wins=player.wins, losses=player.losses)
