import api_validations.validations_request as validations


def get_upper_only(text: str):
    sc_letter, th_letter = text[1], text[2]
    upper_letters = "".join(letter for letter in text if str(letter).isupper())
    return upper_letters[0] + sc_letter + th_letter + upper_letters[1:]


def copy_validations_to_database():
    for validation in validations.__all__:
        validation_pydantic = getattr(validations, validation)
        model_dict = {
            "model_owner": "system",
            "model_type": "BaseModel",
            "model_name": validation_pydantic.__name__,
            "model_description": "",
            "model_code": get_upper_only(validation_pydantic.__name__),
            "is_confirmed": True,
            "deleted": False,
            "active": True,
        }
        # created_model = Models.find_or_create(**model_dict)
        fields = validation_pydantic.model_fields
        for field, info in fields.items():
            default_value = (
                None
                if getattr(info, "default").__str__() == "PydanticUndefined"
                else getattr(info, "default")
            )
            model_field_dict = {
                "field_name": field,
                "field_type": str(info.annotation),
                "field_required": bool(info.is_required()),
                "model_id": created_model.id,
                "is_confirmed": True,
                "deleted": False,
                "active": True,
            }
            if default_value:
                model_field_dict.update({"field_default_value": str(default_value)})
            # ModelEntities.find_or_create(**model_field_dict)
