from datetime import datetime
from pydantic import BaseModel, validator
from typing import Optional


"""
Add all common fields for all models
"""
class CoreModel(BaseModel):
    class Config:
        allow_population_by_field_name = True


class AuditModelMixin(BaseModel):
    created_at: Optional[datetime]
    updated_at: Optional[datetime]

    @validator('created_at', 'updated_at', pre=True)
    def default_datetime(cls, value: datetime) -> datetime:
        return value or datetime.now()


class IDModelMixin(BaseModel):
    id: int

