import typing as t

if t.TYPE_CHECKING:
    from ..core.discussions import DB_Discussion
    from ..core.users import DB_User

from datetime import datetime

from sqlmodel import SQLModel, Field, Relationship



class DB_TagUser(SQLModel, table=True):
    """
        Represents a tag-to-user relationship in the database.
    """

    __tablename__ = 'tag_user'
    user_id: t.Optional[int] = Field(default=None, primary_key=True, foreign_key='users.id')
    """The ID of the user."""
    tag_id: t.Optional[int] = Field(default=None, primary_key=True, foreign_key='tags.id')
    """The ID of the tag."""

    marked_as_read_at: t.Optional[datetime]
    """When the user marked the tag as read?"""
    is_hidden: bool = Field(default=False)
    """?"""



class DB_Tag(SQLModel, table=True):
    """
        Represents a tag in the database.
    """

    __tablename__ = 'tags'
    id: t.Optional[int] = Field(default=None, primary_key=True)
    """The ID of the tag. This is handled by the database."""

    name: str = Field(max_length=100)
    """The name of the tag."""
    slug: str = Field(max_length=100)
    """The tag's slug (will be used in URL)."""

    description: t.Optional[t.Text]
    """The description of the tag."""
    color: t.Optional[str] = Field(max_length=50)
    """The tag's color."""

    background_path: t.Optional[str] = Field(max_length=100)
    """?"""
    background_mode: t.Optional[str] = Field(max_length=100)
    """?"""

    position: t.Optional[int]
    """The tag's position in the tag tree."""
    parent_id: t.Optional[int] = Field(default=None, foreign_key='tags.id')
    """The ID of the parent tag."""
    parent_tag: t.Optional['DB_Tag'] = Relationship(back_populates='children')
    """The tag's parent tag."""

    default_sort: t.Optional[str]
    """The default sorting behaviour of the tag."""

    is_restricted: bool = Field(default=False)
    """Whether or not the tag is restricted."""
    is_hidden: bool = Field(default=False)
    """Whether or not the tag is hidden."""

    discussion_count: int = Field(default=0)
    """How many discussions are tagged with this tag?"""

    last_posted_at: t.Optional[datetime]
    """The datetime when was the last discussion posted in this tag."""
    last_posted_discussion_id: t.Optional[int] = Field(default=None, foreign_key='discussions.id')
    """The ID of the last posted discussion in this tag."""
    last_posted_discussion: t.Optional['DB_Discussion'] = Relationship(back_populates='tags')
    """The last posted discussion in this tag."""
    last_posted_user_id: t.Optional[int] = Field(default=None, foreign_key='users.id')
    """The ID of the user that last posted in this tag."""
    last_posted_user: t.Optional['DB_User'] = Relationship(back_populates='tags')
    """The user that last posted in this tag."""

    icon: t.Optional[str] = Field(max_length=100)
    """The [FontAwesome](https://fontawesome.com/v5.15/icons?d=gallery&m=free) icon for the tag."""

    users: t.List['DB_User'] = Relationship(back_populates='tags', link_model=DB_TagUser)
    """Users that have relationship with this tag."""

sqlmodel.Field sqlmodel.Relationship
[((394, 455), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)', 'foreign_key': '"""users.id"""'}), "(default=None, primary_key=True, foreign_key='users.id')\n", (399, 455), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((516, 576), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)', 'foreign_key': '"""tags.id"""'}), "(default=None, primary_key=True, foreign_key='tags.id')\n", (521, 576), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((721, 741), 'sqlmodel.Field', 'Field', ([], {'default': '(False)'}), '(default=False)\n', (726, 741), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((905, 942), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (910, 942), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1022, 1043), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (1027, 1043), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1091, 1112), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (1096, 1112), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1265, 1285), 'sqlmodel.Field', 'Field', ([], {'max_length': '(50)'}), '(max_length=50)\n', (1270, 1285), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1353, 1374), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (1358, 1374), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1426, 1447), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (1431, 1447), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1570, 1612), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""tags.id"""'}), "(default=None, foreign_key='tags.id')\n", (1575, 1612), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1688, 1727), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""children"""'}), "(back_populates='children')\n", (1700, 1727), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1874, 1894), 'sqlmodel.Field', 'Field', ([], {'default': '(False)'}), '(default=False)\n', (1879, 1894), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1965, 1985), 'sqlmodel.Field', 'Field', ([], {'default': '(False)'}), '(default=False)\n', (1970, 1985), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2059, 2075), 'sqlmodel.Field', 'Field', ([], {'default': '(0)'}), '(default=0)\n', (2064, 2075), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2296, 2345), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""discussions.id"""'}), "(default=None, foreign_key='discussions.id')\n", (2301, 2345), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2464, 2499), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""tags"""'}), "(back_populates='tags')\n", (2476, 2499), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2593, 2636), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""users.id"""'}), "(default=None, foreign_key='users.id')\n", (2598, 2636), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2742, 2777), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""tags"""'}), "(back_populates='tags')\n", (2754, 2777), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((2856, 2877), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (2861, 2877), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((3010, 3068), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""tags"""', 'link_model': 'DB_TagUser'}), "(back_populates='tags', link_model=DB_TagUser)\n", (3022, 3068), False, 'from sqlmodel import SQLModel, Field, Relationship\n')]