from fastapi import APIRouter, Depends
from ..utils import engine, get_session
from sqlmodel import Session, select, or_
from ..models.client import Client
from ..models.sponsor import Sponsor
from sqlalchemy.exc import NoResultFound
from datetime import datetime

router = APIRouter(prefix="/api/sponsors", tags=["sponsor"])


@router.post("/")
async def post_sponsor(
    *,
    sponsor: Sponsor,
    session: Session = Depends(get_session),
):
    """
    Post new sponsor.

    Parameters
    ----------
    sponsor : Sponsor
        Sponsor that is to be added to the database.
    session : Session
        SQL session that is to be used to add the sponsor.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Sponsor).where(
        or_(Sponsor.name == sponsor.name, Sponsor.id == sponsor.id)
    )
    try:
        result = session.exec(statement).one()
        return False
    except NoResultFound:
        session.add(sponsor)
        session.commit()
        session.refresh(sponsor)
        return sponsor


@router.get("/")
async def get_sponsor_list(
    session: Session = Depends(get_session), is_active: bool = None
):
    """
    Get entire sponsor list (enabled and disabled).

    Parameters
    ----------
    session : Session
        SQL session that is to be used to get a list of the sponsors.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(
        Sponsor.id,
        Sponsor.client_id,
        Sponsor.name.label("sponsor_name"),
        Sponsor.short_name.label("sponsor_short_name"),
        Client.name.label("client_name"),
        Sponsor.is_active,
    ).join(Client)
    if is_active != None:
        statement_final = statement.where(Sponsor.is_active == is_active).order_by(
            Sponsor.is_active.desc()
        )
    else:
        statement_final = statement.order_by(Sponsor.is_active.desc())
    results = session.exec(statement_final).all()
    return results


@router.get("/{sponsor_name}")
async def read_teams(sponsor_name: str = None, session: Session = Depends(get_session)):
    """
    Read the contents of a given sponsor.

    Parameters
    ----------
    sponsor_name : str
        Name of sponsor to be read.
    session : Session
        SQL session that is to be used to read a sponsor.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Sponsor).where(Sponsor.name == sponsor_name)
    try:
        result = session.exec(statement).one()
        return result
    except NoResultFound:
        msg = f"""There is no sponsor named {sponsor_name}"""


@router.get("/{sponsor_id}/client-name")
async def get_client_name_by_sponsor_id(
    sponsor_id: int, session: Session = Depends(get_session)
):
    """
    Get client name by sponsor id.

    Parameters
    ----------
    sponsor_id : int
        ID of sponsor to pull client name from.
    session : Session
        SQL session that is to be used to get the client name.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = (
        select(Sponsor.id, Client.id, Client.name)
        .join(Client)
        .where(Sponsor.id == sponsor_id)
        .where(Client.is_active == True)
    )
    result = session.exec(statement).one()
    return result


@router.put("/{sponsor_id}/activate")
async def activate_sponsor(
    sponsor_id: int = None,
    session: Session = Depends(get_session),
):
    """
    Activate a sponsor.

    Parameters
    ----------
    sponsor_name : str
        Name of sponsor to be activated.
    session : Session
        SQL session that is to be used to activate the sponsor.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Sponsor).where(Sponsor.id == sponsor_id)
    sponsor_to_activate = session.exec(statement).one()
    sponsor_to_activate.is_active = True
    sponsor_to_activate.updated_at = datetime.now()
    session.add(sponsor_to_activate)
    session.commit()
    session.refresh(sponsor_to_activate)
    return sponsor_to_activate


@router.put("/{sponsor_id}/deactivate")
async def deactivate_sponsor(
    sponsor_id: int = None,
    session: Session = Depends(get_session),
):
    """
    Deactivate a sponsor.

    Parameters
    ----------
    sponsor_name : str
        Name of sponsor to be deactivated.
    session : Session
        SQL session that is to be used to deactivate the sponsor.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Sponsor).where(Sponsor.id == sponsor_id)
    sponsor_to_deactivate = session.exec(statement).one()
    sponsor_to_deactivate.is_active = False
    sponsor_to_deactivate.updated_at = datetime.now()
    session.add(sponsor_to_deactivate)
    session.commit()
    session.refresh(sponsor_to_deactivate)
    return sponsor_to_deactivate


@router.put("/")
async def update_sponsor(
    id: int,
    new_client_id: int = None,
    new_name: str = None,
    new_short_name: str = None,
    is_active: bool = None,
    session: Session = Depends(get_session),
):
    """
    Update a sponsor.

    Parameters
    ----------
    id : int
        ID of sponsor to be updated.
    client_id : int
        Updated client ID.
    name : str
        Updated sponsor name.
    short_name : str
        Updated short name of sponsor.
    is_active : bool
        Updated status of sponsor.
    session : Session
        SQL session that is to be used to update the sponsor.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Sponsor).where(Sponsor.id == id)
    sponsor_to_update = session.exec(statement).one()
    if new_name != None:
        sponsor_to_update.name = new_name
    if new_short_name != None:
        sponsor_to_update.short_name = new_short_name
    if new_client_id != None:
        sponsor_to_update.client_id = new_client_id
    session.add(sponsor_to_update)
    sponsor_to_update.updated_at = datetime.now()
    session.commit()
    session.refresh(sponsor_to_update)
    return sponsor_to_update

sqlmodel.select sqlmodel.or_ fastapi.Depends datetime.datetime.now fastapi.APIRouter
[((274, 325), 'fastapi.APIRouter', 'APIRouter', ([], {'prefix': '"""/api/sponsors"""', 'tags': "['sponsor']"}), "(prefix='/api/sponsors', tags=['sponsor'])\n", (283, 325), False, 'from fastapi import APIRouter, Depends\n'), ((422, 442), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (429, 442), False, 'from fastapi import APIRouter, Depends\n'), ((1144, 1164), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1151, 1164), False, 'from fastapi import APIRouter, Depends\n'), ((2124, 2144), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (2131, 2144), False, 'from fastapi import APIRouter, Depends\n'), ((2809, 2829), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (2816, 2829), False, 'from fastapi import APIRouter, Depends\n'), ((3504, 3524), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (3511, 3524), False, 'from fastapi import APIRouter, Depends\n'), ((4024, 4038), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (4036, 4038), False, 'from datetime import datetime\n'), ((4292, 4312), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (4299, 4312), False, 'from fastapi import APIRouter, Depends\n'), ((4825, 4839), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (4837, 4839), False, 'from datetime import datetime\n'), ((5174, 5194), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (5181, 5194), False, 'from fastapi import APIRouter, Depends\n'), ((6100, 6114), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (6112, 6114), False, 'from datetime import datetime\n'), ((795, 854), 'sqlmodel.or_', 'or_', (['(Sponsor.name == sponsor.name)', '(Sponsor.id == sponsor.id)'], {}), '(Sponsor.name == sponsor.name, Sponsor.id == sponsor.id)\n', (798, 854), False, 'from sqlmodel import Session, select, or_\n'), ((764, 779), 'sqlmodel.select', 'select', (['Sponsor'], {}), '(Sponsor)\n', (770, 779), False, 'from sqlmodel import Session, select, or_\n'), ((2467, 2482), 'sqlmodel.select', 'select', (['Sponsor'], {}), '(Sponsor)\n', (2473, 2482), False, 'from sqlmodel import Session, select, or_\n'), ((3842, 3857), 'sqlmodel.select', 'select', (['Sponsor'], {}), '(Sponsor)\n', (3848, 3857), False, 'from sqlmodel import Session, select, or_\n'), ((4636, 4651), 'sqlmodel.select', 'select', (['Sponsor'], {}), '(Sponsor)\n', (4642, 4651), False, 'from sqlmodel import Session, select, or_\n'), ((5702, 5717), 'sqlmodel.select', 'select', (['Sponsor'], {}), '(Sponsor)\n', (5708, 5717), False, 'from sqlmodel import Session, select, or_\n'), ((3171, 3213), 'sqlmodel.select', 'select', (['Sponsor.id', 'Client.id', 'Client.name'], {}), '(Sponsor.id, Client.id, Client.name)\n', (3177, 3213), False, 'from sqlmodel import Session, select, or_\n')]