from datetime import datetime

<<<<<<<< HEAD:kontor-schema/src/kontor_schema/admin.py
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.dialects.mysql import BIT
========
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
>>>>>>>> 934ef82 (Resolve "evaluate uv"):kontor-api/src/schema/admin.py
from sqlalchemy.orm import relationship, mapped_column, Mapped

from .base import Base, BaseMixin


class User(Base, BaseMixin):
    __tablename__ = 'user'
    first_name = Column(String(255))
    last_name = Column(String(255))
    user_name = Column(String(255), nullable=False)
    email = Column(String(255))
    password = Column(String(255))
    enabled = Column(BIT(1))
    matrix = relationship("AuthorizationMatrix")
    tokens = relationship("Token")

    def get_full_name(self) -> str:
        full_name = ""
        if self.first_name is not None:
            full_name += self.first_name
        if self.last_name is not None:
            if len(full_name) > 0:
                full_name += " "
            full_name += self.last_name
        return full_name


class Token(Base, BaseMixin):
    __tablename__ = "token"
    token = Column(String(255), nullable=False, unique=True)
    name = Column(String(255))
    last_used_date: Mapped[datetime] = mapped_column()
    enabled = Column(BIT(1))
    user_id = Column(String(255), ForeignKey("user.id"), nullable=False)
    user = relationship("User", back_populates="tokens")


class Role(Base, BaseMixin):
    __tablename__ = "role"
    name = Column(String(255), nullable=False)
    matrix = relationship("AuthorizationMatrix")


class AuthorizationMatrix(Base, BaseMixin):
    __tablename__ = "authorization_matrix"
    user_id = Column(String, ForeignKey("user.id"), nullable=False)
    user = relationship("User", back_populates="matrix")
    role_id = Column(String, ForeignKey("role.id"), nullable=False)
    role = relationship("Role", back_populates="matrix")


class ModuleData(Base, BaseMixin):
    __tablename__ = "module_data"
    module_name = Column(String(255), nullable=False)
    import_data = Column(BIT(1))


class MailAccount(Base, BaseMixin):
    __tablename__ = "mail_account"
    host = Column(String(255))
    port = Column(Integer)
    protocol = Column(String(255))
    user_name = Column(String(255))
    password = Column(String(255))
    start_tls = Column(BIT(1))


class Mail(Base, BaseMixin):
    __tablename__ = "mail"
    folder: Mapped[str] = mapped_column()
    subject: Mapped[str] = mapped_column()
    body: Mapped[str] = mapped_column()
    sent_date: Mapped[datetime] = mapped_column()
    received_date: Mapped[datetime] = mapped_column()
