from datetime import datetime

from sqlalchemy import DateTime
from sqlalchemy.orm import declarative_base, Mapped, mapped_column, QueryPropertyDescriptor
from text_unidecode import unidecode

from backend.db.db_setup import SessionLocal

Model = declarative_base()

class SqlAlchemyBase(Model):
  __abstract__ = True
  query: QueryPropertyDescriptor = SessionLocal.query_property()

  created_at: Mapped[datetime | None] = mapped_column(DateTime, default=datetime.utcnow(), index=True)
  updated_at: Mapped[datetime | None] = mapped_column(DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())

  @classmethod
  def normalize(cls, val: str) -> str:
    return unidecode(val).lower().strip()
