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

from databases import OccupantTypes
from api_validations.validations_request import (
    SingleOccupantTypeClassKey,
    SingleOccupantTypeUUID,
)


occupant_types_route = APIRouter(prefix="/occupant_types", tags=["Occupant Types"])
occupant_types_route.include_router(occupant_types_route, include_in_schema=False)


@occupant_types_route.post(
    path="/get/code", summary="Get single occupant type via code"
)
def get_single_occupant_type_via_code(data: SingleOccupantTypeClassKey):
    occupant_type = OccupantTypes.query.filter(
        OccupantTypes.occupant_code.ilike(data.type_code)
    ).populate_existing()
    if record := occupant_type.first():
        return JSONResponse(
            content={
                "completed": True,
                "message": "Get single occupant type via code",
                "data": record.get_dict() if record else None,
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Occupant type not found",
    )


@occupant_types_route.post(
    path="/get/uu_id", summary="Get single occupant type via uu_id"
)
def get_single_occupant_type_via_uuid(data: SingleOccupantTypeUUID):
    occupant_type = OccupantTypes.query.filter(
        OccupantTypes.uu_id == data.uu_id
    ).populate_existing()
    if records := occupant_type.first():
        return JSONResponse(
            content={
                "completed": True,
                "message": "Get single occupant type via uu_id",
                "data": records.get_dict(),
            },
            status_code=status.HTTP_200_OK,
        )
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Occupant type not found",
    )
