from fastapi import APIRouter, Depends
from ..utils import engine, get_session
from sqlmodel import Session, select, or_
from ..models.team import Team
from ..models.user import User
from sqlalchemy.exc import NoResultFound
from datetime import datetime

router = APIRouter(prefix="/api/teams", tags=["team"])
session = Session(engine)


@router.post("/")
async def post_team(
    *,
    team: Team,
    session: Session = Depends(get_session),
):
    """Post new team"""
    statement = select(Team).where(or_(Team.name == team.name, Team.id == team.id))
    try:
        result = session.exec(statement).one()
        return False
    except NoResultFound:
        session.add(team)
        session.commit()
        session.refresh(team)
        return team


@router.get("/")
async def get_team_list(session: Session = Depends(get_session)):
    """Get team list"""
    statement = select(Team)
    results = session.exec(statement).all()
    return results


@router.get("/active")
async def get_active_team_list(session: Session = Depends(get_session)):
    """Get list of active teams"""
    statement = (
        select(
            Team.id,
            Team.lead_user_id,
            Team.name.label("team_name"),
            Team.short_name.label("team_short_name"),
            User.id,
            User.short_name.label("user_name"),
        )
        .join(User)
        .where(Team.is_active == True)
    )
    results = session.exec(statement).all()
    return results


@router.get("/{team_name}")
async def read_teams(team_name: str = None, session: Session = Depends(get_session)):
    """Read the contents of a given team"""
    statement = select(Team).where(Team.name == team_name)
    try:
        result = session.exec(statement).one()
        return result
    except NoResultFound:
        msg = f"""There is no team named {team_name}"""
        return msg


@router.get("/{team_id}/user-name")
async def get_user_name_by_team_id(
    team_id: int, session: Session = Depends(get_session)
):
    """Get user name by team id"""
    statement = (
        select(Team.id, User.id, User.name)
        .join(User)
        .where(Team.id == team_id)
        .where(User.active == True)
    )
    result = session.exec(statement).one()
    return result


@router.put("/{team_name}/activate")
async def activate_team(
    team_name: str = None,
    session: Session = Depends(get_session),
):
    """Activate team"""
    statement = select(Team).where(Team.name == team_name)
    team_to_activate = session.exec(statement).one()
    team_to_activate.is_active = True
    team_to_activate.updated_at = datetime.now()
    session.add(team_to_activate)
    session.commit()
    session.refresh(team_to_activate)
    return team_to_activate


@router.put("/{team_name}/deactivate")
async def deactivate_team(
    team_name: str = None,
    session: Session = Depends(get_session),
):
    """Deactivate team"""
    statement = select(Team).where(Team.name == team_name)
    team_to_deactivate = session.exec(statement).one()
    team_to_deactivate.is_active = False
    team_to_deactivate.updated_at = datetime.now()
    session.add(team_to_deactivate)
    session.commit()
    session.refresh(team_to_deactivate)
    return team_to_deactivate


@router.put("/")
async def update_team(
    id: str = None,
    lead_user_id: str = None,
    name: str = None,
    is_active: bool = None,
    session: Session = Depends(get_session),
):
    """Update team"""
    statement = select(Team).where(or_(Team.name == name, Team.id == id))
    team_to_update = session.exec(statement).one()
    team_to_update.lead_user_id = lead_user_id
    team_to_update.name = name
    team_to_update.is_active = is_active
    session.add(team_to_update)
    team_to_update.updated_at = datetime.now()
    session.commit()
    session.refresh(team_to_update)
    return team_to_update

datetime.datetime.now sqlmodel.Session fastapi.Depends sqlmodel.or_ sqlmodel.select fastapi.APIRouter
[((264, 309), 'fastapi.APIRouter', 'APIRouter', ([], {'prefix': '"""/api/teams"""', 'tags': "['team']"}), "(prefix='/api/teams', tags=['team'])\n", (273, 309), False, 'from fastapi import APIRouter, Depends\n'), ((320, 335), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (327, 335), False, 'from sqlmodel import Session, select, or_\n'), ((423, 443), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (430, 443), False, 'from fastapi import APIRouter, Depends\n'), ((822, 842), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (829, 842), False, 'from fastapi import APIRouter, Depends\n'), ((885, 897), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (891, 897), False, 'from sqlmodel import Session, select, or_\n'), ((1036, 1056), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1043, 1056), False, 'from fastapi import APIRouter, Depends\n'), ((1576, 1596), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1583, 1596), False, 'from fastapi import APIRouter, Depends\n'), ((1992, 2012), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1999, 2012), False, 'from fastapi import APIRouter, Depends\n'), ((2385, 2405), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (2392, 2405), False, 'from fastapi import APIRouter, Depends\n'), ((2618, 2632), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (2630, 2632), False, 'from datetime import datetime\n'), ((2872, 2892), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (2879, 2892), False, 'from fastapi import APIRouter, Depends\n'), ((3114, 3128), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (3126, 3128), False, 'from datetime import datetime\n'), ((3421, 3441), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (3428, 3441), False, 'from fastapi import APIRouter, Depends\n'), ((3776, 3790), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (3788, 3790), False, 'from datetime import datetime\n'), ((507, 554), 'sqlmodel.or_', 'or_', (['(Team.name == team.name)', '(Team.id == team.id)'], {}), '(Team.name == team.name, Team.id == team.id)\n', (510, 554), False, 'from sqlmodel import Session, select, or_\n'), ((3503, 3540), 'sqlmodel.or_', 'or_', (['(Team.name == name)', '(Team.id == id)'], {}), '(Team.name == name, Team.id == id)\n', (3506, 3540), False, 'from sqlmodel import Session, select, or_\n'), ((488, 500), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (494, 500), False, 'from sqlmodel import Session, select, or_\n'), ((1659, 1671), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (1665, 1671), False, 'from sqlmodel import Session, select, or_\n'), ((2450, 2462), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (2456, 2462), False, 'from sqlmodel import Session, select, or_\n'), ((2939, 2951), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (2945, 2951), False, 'from sqlmodel import Session, select, or_\n'), ((3484, 3496), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (3490, 3496), False, 'from sqlmodel import Session, select, or_\n'), ((2077, 2112), 'sqlmodel.select', 'select', (['Team.id', 'User.id', 'User.name'], {}), '(Team.id, User.id, User.name)\n', (2083, 2112), False, 'from sqlmodel import Session, select, or_\n')]