from datetime import datetime

from beanie import Document
from pydantic import BaseModel, Field, validator


class CrudDocument(Document):
    _id: str
    created_at: datetime = Field(default=datetime.utcnow(), nullable=False, title="Créé le")
    updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False, title="Modifié le")

    @validator("label", always=True, check_fields=False)
    def generate_label(cls, v, values, **kwargs):
        return v

    class Settings:
        fulltext_search = []


def text_area(*args, **kwargs):
    kwargs['widget'] = {
        "formlyConfig": {
            "type": "textarea",
            "props": {
                "placeholder": "Leaving this field empty will cause formData property to be `null`",
                "rows": kwargs['size'] if 'size' in kwargs else 10
            }
        }
    }

    return Field(*args, **kwargs)


def RichtextMultiline(*args, **kwargs):
    if 'props' not in kwargs:
        kwargs['props'] = {}

    kwargs['props']['richtext'] = True
    kwargs['props']['multiline'] = True

    return Field(*args, **kwargs)


def RichtextSingleline(*args, **kwargs):
    if 'props' not in kwargs:
        kwargs['props'] = {}

    kwargs['props']['richtext'] = True
    kwargs['props']['multiline'] = False

    return Field(*args, **kwargs)


class DictionaryEntry(BaseModel):
    key: str
    value: str = ""
