"""
Team Router - Refactored
Thin HTTP layer using TeamService for business logic.
"""

from fastapi import APIRouter, Query, Response, Depends
from typing import List, Optional, Literal

from ..dependencies import oauth2_scheme, PRIVATE_IN_SCHEMA
from .base import BaseService
from .team_service import TeamService

router = APIRouter(prefix='/api/v3/teams', tags=['teams'])


@router.get('')
async def get_teams(
    season: Optional[int] = None,
    owner_id: list = Query(default=None),
    manager_id: list = Query(default=None),
    team_abbrev: list = Query(default=None),
    active_only: Optional[bool] = False,
    short_output: Optional[bool] = False,
    csv: Optional[bool] = False
):
    """Get teams with filtering."""
    result = TeamService.get_teams(
        season=season,
        owner_id=owner_id if owner_id else None,
        manager_id=manager_id if manager_id else None,
        team_abbrev=team_abbrev if team_abbrev else None,
        active_only=active_only or False,
        short_output=short_output or False,
        as_csv=csv or False
    )
    
    if csv:
        return Response(content=result, media_type='text/csv')
    return result


@router.get('/{team_id}')
async def get_one_team(team_id: int):
    """Get a single team by ID."""
    return TeamService.get_team(team_id)


@router.get('/{team_id}/roster/{which}')
async def get_team_roster(
    team_id: int,
    which: Literal['current', 'next'],
    sort: Optional[str] = None
):
    """Get team roster with IL lists."""
    return TeamService.get_team_roster(team_id, which, sort=sort)


@router.patch('/{team_id}')
async def patch_team(
    team_id: int,
    token: str = Depends(oauth2_scheme),
    manager1_id: Optional[int] = None,
    manager2_id: Optional[int] = None,
    gmid: Optional[int] = None,
    gmid2: Optional[int] = None,
    mascot: Optional[str] = None,
    stadium: Optional[str] = None,
    thumbnail: Optional[str] = None,
    color: Optional[str] = None,
    abbrev: Optional[str] = None,
    sname: Optional[str] = None,
    lname: Optional[str] = None,
    dice_color: Optional[str] = None,
    division_id: Optional[int] = None,
):
    """Patch a team (partial update)."""
    # Build dict of provided fields
    data = {}
    locals_dict = locals()
    for key, value in locals_dict.items():
        if key not in ('team_id', 'token') and value is not None:
            data[key] = value
    
    return TeamService.update_team(team_id, data, token)


@router.post('')
async def post_teams(
    team_list: dict,
    token: str = Depends(oauth2_scheme)
):
    """Create multiple teams."""
    return TeamService.create_teams(team_list.get("teams", []), token)


@router.delete('/{team_id}')
async def delete_team(
    team_id: int,
    token: str = Depends(oauth2_scheme)
):
    """Delete a team."""
    return TeamService.delete_team(team_id, token)
