from fastapi import APIRouter, Depends
from ..utils import engine, get_session
from sqlmodel import Session, select, SQLModel, or_
from sqlalchemy.exc import NoResultFound
from ..models.role import Role
from datetime import datetime

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

# Post new role
@router.post("/")
async def post_role(*, role: Role, session: Session = Depends(get_session)):
    statement = select(Role).where(Role.id == role.id)
    try:
        result = session.exec(statement).one()
        return False
    except NoResultFound:
        session.add(role)
        session.commit()
        session.refresh(role)
        return role


# Get list of all roles
@router.get("/")
async def read_roles(session: Session = Depends(get_session)):
    statement = select(Role)
    results = session.exec(statement).all()
    return results


# Get list of active roles
@router.get("/active")
async def read_roles(session: Session = Depends(get_session)):
    statement = select(Role).where(Role.is_active == True)
    results = session.exec(statement).all()
    return results


@router.put("/{role_id}/activate")
async def activate_role(
    role_id: str = None,
    session: Session = Depends(get_session),
):
    statement = select(Role).where(Role.id == role_id)
    role_to_activate = session.exec(statement).one()
    role_to_activate.is_active = True
    role_to_activate.updated_at = datetime.now()
    session.add(role_to_activate)
    session.commit()
    session.refresh(role_to_activate)
    return role_to_activate


# Deactivate role
@router.put("/{role_id}/deactivate")
async def deactivate_role(
    role_id: str = None,
    session: Session = Depends(get_session),
):
    statement = select(Role).where(Role.id == role_id)
    role_to_deactivate = session.exec(statement).one()
    role_to_deactivate.is_active = False
    role_to_deactivate.updated_at = datetime.now()
    session.add(role_to_deactivate)
    session.commit()
    session.refresh(role_to_deactivate)
    return role_to_deactivate


# Update role
@router.put("/")
async def update_role(
    id: str = None,
    new_name: str = None,
    new_short_name: str = None,
    is_active: bool = None,
    session: Session = Depends(get_session),
):
    statement = select(Role.is_active).where(Role.id == id)
    result = session.exec(statement).first()
    if result == True:
        statement = select(Role).where(Role.id == id)
        role_to_update = session.exec(statement).one()
        if new_name != None:
            role_to_update.name = new_name
        if new_short_name != None:
            role_to_update.short_name = new_short_name
        if is_active != None:
            role_to_update.is_active = is_active
        session.add(role_to_update)
        role_to_update.updated_at = datetime.now()
        session.commit()
        session.refresh(role_to_update)
        return role_to_update
    else:
        return False

datetime.datetime.now sqlmodel.Session fastapi.Depends sqlmodel.select fastapi.APIRouter
[((243, 288), 'fastapi.APIRouter', 'APIRouter', ([], {'prefix': '"""/api/roles"""', 'tags': "['role']"}), "(prefix='/api/roles', tags=['role'])\n", (252, 288), False, 'from fastapi import APIRouter, Depends\n'), ((299, 314), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (306, 314), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((404, 424), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (411, 424), False, 'from fastapi import APIRouter, Depends\n'), ((769, 789), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (776, 789), False, 'from fastapi import APIRouter, Depends\n'), ((808, 820), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (814, 820), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((976, 996), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (983, 996), False, 'from fastapi import APIRouter, Depends\n'), ((1231, 1251), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1238, 1251), False, 'from fastapi import APIRouter, Depends\n'), ((1436, 1450), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (1448, 1450), False, 'from datetime import datetime\n'), ((1704, 1724), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1711, 1724), False, 'from fastapi import APIRouter, Depends\n'), ((1916, 1930), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (1928, 1930), False, 'from datetime import datetime\n'), ((2243, 2263), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (2250, 2263), False, 'from fastapi import APIRouter, Depends\n'), ((2818, 2832), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (2830, 2832), False, 'from datetime import datetime\n'), ((443, 455), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (449, 455), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((1015, 1027), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (1021, 1027), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((1272, 1284), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (1278, 1284), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((1745, 1757), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (1751, 1757), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((2284, 2306), 'sqlmodel.select', 'select', (['Role.is_active'], {}), '(Role.is_active)\n', (2290, 2306), False, 'from sqlmodel import Session, select, SQLModel, or_\n'), ((2416, 2428), 'sqlmodel.select', 'select', (['Role'], {}), '(Role)\n', (2422, 2428), False, 'from sqlmodel import Session, select, SQLModel, or_\n')]