import typing
from api_validations.core_validations import BaseModelRegular
from api_validations.validations_request import (
    PydanticBaseModel,
    ListOptions,
)

from datetime import datetime
from pydantic import create_model
from databases import Modules
from fastapi.exceptions import HTTPException


class ConvertField:

    def __init__(self, match, default_val=None):
        self.match = match
        self.default_val = default_val

    def typing_return(self):
        typing_dict = {
            "<class 'float'>": float,
            "<class 'bool'>": bool,
            "<class 'int'>": int,
            "<class 'str'>": str,
            "<class 'dict'>": dict,
            "<class 'list'>": list,
            "<class 'datetime.datetime'>": datetime,
            "typing.Optional[datetime.datetime]": typing.Optional[datetime],
            "typing.Optional[bool]": typing.Optional[bool],
            "typing.Optional[list]": typing.Optional[list],
            "typing.Optional[str]": typing.Optional[str],
            "typing.Optional[int]": typing.Optional[int],
            "typing.Optional[float]": typing.Optional[float],
            "typing.Optional[dict]": typing.Optional[dict],
        }
        matches_with = typing_dict.get(self.match, typing.Optional[str])
        default_value = getattr(self.default_val, "field_default_value", None)
        return matches_with, default_value


#
# def create_model_from_database(model_id: typing.Union[int, str]):
#     if isinstance(model_id, int):
#         selected_model = Models.find_one(id=model_id)
#     else:
#         selected_model = Models.find_one(uu_id=str(model_id))
#
#     if not selected_model:
#         raise HTTPException(
#             status_code=202,
#             detail=f"Model {selected_model.model_name} not found in database. Please add model to api.",
#         )
#     pydantic_class = getattr(root_validates, selected_model.model_type, None)
#     if not pydantic_class:
#         raise HTTPException(
#             status_code=202,
#             detail=f"Pydantic class {selected_model.model_type} not found in database. Please add model to api.",
#         )
#
#     model_entities_records = ModelEntities.filter_all(
#         ModelEntities.model_id == selected_model.id
#     ).data
#
#     if not model_entities_records:
#         raise HTTPException(
#             status_code=202,
#             detail="Model has no entities registered. Please add entities to model.",
#         )
#
#     fields = {}
#     for entity in model_entities_records:
#         fields[entity.field_name] = ConvertField(
#             entity.field_type, entity.field_default_value
#         ).typing_return()
#
#     return create_model(
#         __model_name=selected_model.model_name,  # pydantic_name(User)
#         __module__=pydantic_class.__module__,  # field_name(uu_id)
#         **fields,  # field_name = (field_type (Optional[str]), default_value(None))
#     )
