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

router = APIRouter(prefix="/api/clients", tags=["client"])


@router.post("/")
async def post_client(*, client: Client, session: Session = Depends(get_session)):
    """
    Post a new client.

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


@router.get("/")
async def read_clients(session: Session = Depends(get_session)):
    """
    Get a list of all clients.

    Parameters
    ----------
    session : Session
        SQL session that is to be used to get a list of the clients.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = select(Client)
    results = session.exec(statement).all()
    return results


@router.get("/active")
async def read_clients(session: Session = Depends(get_session)):
    """
    Get a list of all active clients.

    Parameters
    ----------
    session : Session
        SQL session that is to be used to get a list of all of the active clients.
        Defaults to creating a dependency on the running SQL model session.
    """
    statement = 