from fastapi import status
from fastapi.routing import APIRouter
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse

from databases import ApiEnumDropdown
from api_validations.validations_request import (
    SingleEnumClassKey,
    SingleEnumUUID,
    SingleEnumOnlyClass,
)


enums_route = APIRouter(prefix="/enums", tags=["Enums and Dropdowns of API"])
enums_route.include_router(enums_route, include_in_schema=False)


@enums_route.post(path="/get/key", summary="Get single enum via key")
def get_single_enum_via_key_and_class(data: SingleEnumClassKey):
    enum = ApiEnumDropdown.query.filter(
        ApiEnumDropdown.enum_class.ilike(data.class_name),
        ApiEnumDropdown.key.ilike(data.key_name),
    ).populate_existing()
    if record := enum.first():
        return JSONResponse(
            content={
                "completed": True,
                "message": "Get single enum via key",
                "data": record.get_enum_dict() if enum else None,
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Enum not found",
    )


@enums_route.post(path="/get/uu_id", summary="Get single enum via uu_id")
def get_single_enum_via_uuid(data: SingleEnumUUID):
    enum = ApiEnumDropdown.query.filter(
        ApiEnumDropdown.uu_id == data.uu_id
    ).populate_existing()
    if records := enum.first():
        return JSONResponse(
            content={
                "completed": True,
                "message": "Get single enum via uu_id",
                "data": records.get_enum_dict(),
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Enum not found",
    )


@enums_route.post(path="/list/class", summary="Get all enums via class")
def list_all_enums_with_class(data: SingleEnumOnlyClass):
    enums = ApiEnumDropdown.query.filter(
        ApiEnumDropdown.enum_class.ilike(data.class_name or "")
    ).populate_existing()
    if records := enums.all():
        records_list = [record.get_enum_dict() for record in records]
        return JSONResponse(
            content={
                "completed": True,
                "message": "Get all enums via class",
                "count": len(records_list),
                "data": records_list,
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Enums not found",
    )


@enums_route.post(path="/list/all", summary="Get all enums")
def list_all_enums():
    enums = ApiEnumDropdown.query.filter().populate_existing()
    if records := enums.all():
        records_list = [record.get_enum_dict() for record in records]
        return JSONResponse(
            content={
                "completed": True,
                "count": len(records_list),
                "message": "Get all enums",
                "data": records_list,
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Enums can not be listed",
    )
