from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.tysc import Player
from src.db.session import SessionDep
from src.schema.tysc.player import PlayerResponse, to_response


router = APIRouter()

@router.get("/players")
def get_all_players(db: SessionDep) -> List[PlayerResponse]:
    results: List[PlayerResponse] = []
    players = db.query(Player).all()
    for player in players:
        response = to_response(player)
        results.append(response)
    return results

@router.get("/players/{player_id}", response_model=PlayerResponse)
def get_player(player_id: str, db: SessionDep) -> PlayerResponse:
    player = db.get(Player, player_id)
    if player is None:
        raise HTTPException(status_code=404, detail="Player could not be found")
    response = to_response(player)
    return response
