from datetime import date, datetime
from typing import Optional

from sqlalchemy import func, ForeignKey, DateTime
from sqlalchemy.orm import mapped_column, Mapped, relationship

from app.database import Base


class Users(Base):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    email: Mapped[str]
    username: Mapped[str]
    hashed_password: Mapped[str]
    role: Mapped[int]
    black_phoenix: Mapped[bool]
    avatar_image: Mapped[Optional[str]] = mapped_column(server_default='app/static/images/ты уже пешка BP.png')
    date_of_birth: Mapped[date]
    date_of_registration: Mapped[date] = mapped_column(server_default=func.now())

    message = relationship("Messages", back_populates="user")
    usersxchats = relationship("UsersXChats", back_populates="user")
    chat = relationship("Chats", back_populates="user_to_exclude")
    verificationcode = relationship("UsersVerificationCodes", back_populates="user")

    def __str__(self):
        return f"Юзер {self.username}"


class UsersVerificationCodes(Base):
    __tablename__ = "usersverificationcodes"

    id: Mapped[int] = mapped_column(primary_key=True)
    user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
    code: Mapped[str]
    description: Mapped[str]
    date_of_creation: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())

    user = relationship("Users", back_populates="verificationcode")

    def __str__(self):
        return f"Код {self.code} для юзера {self.user}. {self.description}"
