from typing import TYPE_CHECKING
from sqlalchemy.orm import Mapped, mapped_column, relationship

from advanced_alchemy.base import BigIntAuditBase
from advanced_alchemy.types import PasswordHash, HashedPassword
from advanced_alchemy.types.password_hash.pwdlib import PwdlibHasher

from pwdlib.hashers.argon2 import Argon2Hasher as PwdlibArgon2Hasher

if TYPE_CHECKING:
    from chitai.database.models import BookList

    PasswordType = HashedPassword
else:
    PasswordType = str


class User(BigIntAuditBase):
    __tablename__ = "users"

    email: Mapped[str] = mapped_column(unique=True)
    password: Mapped[PasswordType] = mapped_column(
        PasswordHash(backend=PwdlibHasher(hasher=PwdlibArgon2Hasher()))
    )
    shelves: Mapped[list["BookList"]] = relationship(cascade="all, delete-orphan")
