from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.tysc import FieldPosition
from src.db.session import SessionDep
from src.schema.tysc.fieldposition import FieldPositionResponse, to_response


router = APIRouter()

@router.get("/positions")
def get_all_positions(db: SessionDep) -> List[FieldPositionResponse]:
    results: list[FieldPositionResponse] = []
    positions = db.query(FieldPosition).all()
    for position in positions:
        response = to_response(position)
        results.append(response)
    return results

@router.get("/positions/{position_id}", response_model=FieldPositionResponse)
def get_position(position_id: str, db: SessionDep) -> FieldPositionResponse:
    position = db.get(FieldPosition, position_id)
    if position is None:
        raise HTTPException(status_code=404, detail="Fieldposition could not be found")
    response = to_response(position)
    return response
