from typing import List
from fastapi import APIRouter, HTTPException

from src.core.log_conf import logger
from src.db.session import SessionDep
from src.schema.tysc.sport import SportResponse, to_response
from src.db.models.tysc import Sport

router = APIRouter()

@router.get("/sports")
def get_all_sports(db: SessionDep) -> List[SportResponse]:
    results: list[SportResponse] = []
    sports = db.query(Sport).all()
    for sport in sports:
        response = to_response(sport)
        results.append(response)
    return results

@router.get("/sports/{sport_id}", response_model=SportResponse)
def get_sport(sport_id: str, db: SessionDep) -> SportResponse:
    sport = db.get(Sport, sport_id)
    if sport is None:
        raise HTTPException(status_code=404, detail="Sport could not be found")
    logger.debug(f"create SportResponse for {sport}")
    response: SportResponse = to_response(sport)
    return response

