from typing import Optional from fastapi import APIRouter, Depends, UploadFile, File from sqlalchemy.ext.asyncio import AsyncSession from starlette.responses import FileResponse from app.database.session import get_db from app.domain.entities.team import TeamEntity from app.domain.entities.team_logo import TeamLogoEntity from app.infrastructure.dependencies import get_current_user, require_admin from app.infrastructure.teams_service import TeamsService router = APIRouter() @router.get( '/', response_model=list[TeamEntity], summary='Get all teams', description='Returns all teams', ) async def get_all_teams( db: AsyncSession = Depends(get_db), user=Depends(get_current_user), ): teams_service = TeamsService(db) return await teams_service.get_all_teams() @router.get( '/active/', response_model=Optional[TeamEntity], summary='Get active team', description='Returns active team', ) async def get_all_teams( db: AsyncSession = Depends(get_db), ): teams_service = TeamsService(db) return await teams_service.get_active_team() @router.post( '/', response_model=Optional[TeamEntity], summary='Create a new team', description='Creates a new team', ) async def create_team( team: TeamEntity, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): teams_service = TeamsService(db) return await teams_service.create_team(team) @router.put( '/{team_id}/set-active/', response_model=Optional[TeamEntity], summary='Make team active', description='Makes team active', ) async def update_team( team_id: int, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): teams_service = TeamsService(db) return await teams_service.set_active_team(team_id) @router.put( '/{team_id}/', response_model=Optional[TeamEntity], summary='Update a team', description='Updates a team', ) async def update_team( team_id: int, team: TeamEntity, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): teams_service = TeamsService(db) return await teams_service.update_team(team_id, team) @router.delete( '/{team_id}/', response_model=Optional[TeamEntity], summary='Delete a team', description='Delete a team', ) async def delete_team( team_id: int, db: AsyncSession = Depends(get_db), user=Depends(require_admin), ): teams_service = TeamsService(db) return await teams_service.delete_team(team_id) @router.get( "/{team_id}/file/", response_class=FileResponse, summary="Download logo file by team ID", description="Returns the image file for the given team ID", ) async def download_photo_file( team_id: int, db: AsyncSession = Depends(get_db), ): teams_service = TeamsService(db) return await teams_service.get_photo_file_by_team_id(team_id) @router.post( "/{team_id}/upload/", response_model=TeamLogoEntity, summary="Upload a new photo for a profile", description="Uploads a new photo file and associates it with the given profile ID", ) async def upload_photo( team_id: int, file: UploadFile = File(...), db: AsyncSession = Depends(get_db), user=Depends(get_current_user), ): teams_service = TeamsService(db) return await teams_service.upload_photo(team_id, file)