from datetime import datetime
from typing import Optional, TYPE_CHECKING

from pydantic import EmailStr
from sqlmodel import Column, Enum, Field, Relationship

from .base import SQLModelDBBase
from app.shared.access import AccessLevel

if TYPE_CHECKING:
    from .profile import ProfileDB


class UserDB(SQLModelDBBase, table=True):
    __tablename__ = "user"

    id: int | None = Field(default=None, primary_key=True)
    username: str = Field(index=True)
    email: EmailStr = Field(index=True)
    hashed_password: str
    is_confirmed: bool = Field(default=False)
    created_at: datetime = Field(default_factory=datetime.utcnow)
    access_level: AccessLevel = Field(default=AccessLevel.USER,
                                      sa_column=Column(Enum(AccessLevel),
                                                       nullable=False))

    profile: Optional["ProfileDB"] = Relationship(
        back_populates="account",
        sa_relationship_kwargs=dict(uselist=False,
                                    cascade="all, delete",
                                    single_parent=True,
                                    lazy="joined")
    )

sqlmodel.Field sqlmodel.Enum
[((382, 419), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (387, 419), False, 'from sqlmodel import Column, Enum, Field, Relationship\n'), ((440, 457), 'sqlmodel.Field', 'Field', ([], {'index': '(True)'}), '(index=True)\n', (445, 457), False, 'from sqlmodel import Column, Enum, Field, Relationship\n'), ((480, 497), 'sqlmodel.Field', 'Field', ([], {'index': '(True)'}), '(index=True)\n', (485, 497), False, 'from sqlmodel import Column, Enum, Field, Relationship\n'), ((548, 568), 'sqlmodel.Field', 'Field', ([], {'default': '(False)'}), '(default=False)\n', (553, 568), False, 'from sqlmodel import Column, Enum, Field, Relationship\n'), ((596, 634), 'sqlmodel.Field', 'Field', ([], {'default_factory': 'datetime.utcnow'}), '(default_factory=datetime.utcnow)\n', (601, 634), False, 'from sqlmodel import Column, Enum, Field, Relationship\n'), ((754, 771), 'sqlmodel.Enum', 'Enum', (['AccessLevel'], {}), '(AccessLevel)\n', (758, 771), False, 'from sqlmodel import Column, Enum, Field, Relationship\n')]