from typing import List, Dict

from fastapi import APIRouter, Depends, HTTPException, Query
from sqlmodel import Session, select

from app import models
from app.api import deps


router = APIRouter()


@router.post("/", response_model=models.TeamRead)
def create_team(
    *, session: Session = Depends(deps.get_session), team: models.TeamCreate
) -> models.Team:
    db_team = models.Team.from_orm(team)
    session.add(db_team)
    session.commit()
    session.refresh(db_team)
    return db_team


@router.get("/", response_model=List[models.TeamRead])
def read_teams(
    *,
    session: Session = Depends(deps.get_session),
    offset: int = 0,
    limit: int = Query(default=100, lte=100),
) -> List[models.Team]:
    teams = session.exec(select(models.Team).offset(offset).limit(limit)).all()
    return teams


@router.get("/{team_id}", response_model=models.TeamReadWithHeroes)
def read_team(
    *, team_id: int, session: Session = Depends(deps.get_session)
) -> models.Team:
    team = session.get(models.Team, team_id)
    if not team:
        raise HTTPException(status_code=404, detail="Team not found")
    return team


@router.patch("/{team_id}", response_model=models.TeamRead)
def update_team(
    *,
    session: Session = Depends(deps.get_session),
    team_id: int,
    team: models.TeamUpdate,
) -> models.Team:
    db_team = session.get(models.Team, team_id)
    if not db_team:
        raise HTTPException(status_code=404, detail="Team not found")
    team_data = team.dict(exclude_unset=True)
    for key, value in team_data.items():
        setattr(db_team, key, value)
    session.add(db_team)
    session.commit()
    session.refresh(db_team)
    return db_team


@router.delete("/teams/{team_id}")
def delete_team(
    *, session: Session = Depends(deps.get_session), team_id: int
) -> Dict[str, bool]:
    team = session.get(models.Team, team_id)
    if not team:
        raise HTTPException(status_code=404, detail="Team not found")
    session.delete(team)
    session.commit()
    return {"ok": True}

app.models.Team.from_orm fastapi.HTTPException sqlmodel.select fastapi.Query fastapi.Depends fastapi.APIRouter
[((198, 209), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (207, 209), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((310, 335), 'fastapi.Depends', 'Depends', (['deps.get_session'], {}), '(deps.get_session)\n', (317, 335), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((395, 421), 'app.models.Team.from_orm', 'models.Team.from_orm', (['team'], {}), '(team)\n', (415, 421), False, 'from app import models\n'), ((629, 654), 'fastapi.Depends', 'Depends', (['deps.get_session'], {}), '(deps.get_session)\n', (636, 654), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((696, 723), 'fastapi.Query', 'Query', ([], {'default': '(100)', 'lte': '(100)'}), '(default=100, lte=100)\n', (701, 723), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((979, 1004), 'fastapi.Depends', 'Depends', (['deps.get_session'], {}), '(deps.get_session)\n', (986, 1004), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1291, 1316), 'fastapi.Depends', 'Depends', (['deps.get_session'], {}), '(deps.get_session)\n', (1298, 1316), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1837, 1862), 'fastapi.Depends', 'Depends', (['deps.get_session'], {}), '(deps.get_session)\n', (1844, 1862), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1103, 1158), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""Team not found"""'}), "(status_code=404, detail='Team not found')\n", (1116, 1158), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1471, 1526), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""Team not found"""'}), "(status_code=404, detail='Team not found')\n", (1484, 1526), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((1979, 2034), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""Team not found"""'}), "(status_code=404, detail='Team not found')\n", (1992, 2034), False, 'from fastapi import APIRouter, Depends, HTTPException, Query\n'), ((776, 795), 'sqlmodel.select', 'select', (['models.Team'], {}), '(models.Team)\n', (782, 795), False, 'from sqlmodel import Session, select\n')]