import typing as t

from sqlmodel import SQLModel, Field, Relationship
from datetime import datetime

from .users import DB_User



class DB_AccessToken(SQLModel, table=True):
    __tablename__ = 'access_tokens'
    id: t.Optional[int] = Field(default=None, primary_key=True)
    """The ID of the access token. This is handled by the database."""

    token: str = Field(max_length=40, sa_column_kwargs={'unique': True})
    """The unique access token."""
    user_id: t.Optional[int] = Field(default=None, foreign_key="users.id")
    user: DB_User = Relationship(back_populates="access_tokens")
    """To what user does this token belong to."""

    last_activity_at: datetime
    """When was the access token last active."""
    created_at: datetime
    """When was this token created at?"""

    type: str = Field(max_length=100)
    """The type of the access token (example: `'session_remember'`)"""

    title: t.Optional[str] = Field(max_length=150)
    """The title of the access token."""
    last_ip_address: t.Optional[str] = Field(max_length=45)
    """The last IP address associated with this access token."""
    last_user_agent: t.Optional[str] = Field(max_length=255)
    """The last browser's user agent that used this token."""



class DB_APIKey(SQLModel, table=True):
    __tablename__ = 'api_keys'
    id: t.Optional[int] = Field(default=None, primary_key=True)
    """The ID of the API key. This is handled by the database."""

    key: str = Field(max_length=100)
    """The unique API key."""

    allowedips: t.Optional[str] = Field(max_length=255)
    """The IP addresses that are allowed to use this API key."""
    scopes: t.Optional[str] = Field(max_length=255)
    """The scopes that this API key has access to."""
    user_id: t.Optional[int] = Field(default=None, foreign_key="users.id")
    user: DB_User = Relationship(back_populates="api_keys")
    """As what user to perform actions when using this API key."""

    created_at: datetime = Field(default=datetime.utcnow())
    """When was this API key created at?"""
    last_activity_at: t.Optional[datetime]
    """When was the API key last active?"""

sqlmodel.Relationship sqlmodel.Field datetime.datetime.utcnow
[((238, 275), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (243, 275), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((365, 420), 'sqlmodel.Field', 'Field', ([], {'max_length': '(40)', 'sa_column_kwargs': "{'unique': True}"}), "(max_length=40, sa_column_kwargs={'unique': True})\n", (370, 420), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((487, 530), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""users.id"""'}), "(default=None, foreign_key='users.id')\n", (492, 530), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((551, 595), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""access_tokens"""'}), "(back_populates='access_tokens')\n", (563, 595), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((811, 832), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (816, 832), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((934, 955), 'sqlmodel.Field', 'Field', ([], {'max_length': '(150)'}), '(max_length=150)\n', (939, 955), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1036, 1056), 'sqlmodel.Field', 'Field', ([], {'max_length': '(45)'}), '(max_length=45)\n', (1041, 1056), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1161, 1182), 'sqlmodel.Field', 'Field', ([], {'max_length': '(255)'}), '(max_length=255)\n', (1166, 1182), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1344, 1381), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (1349, 1381), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1464, 1485), 'sqlmodel.Field', 'Field', ([], {'max_length': '(100)'}), '(max_length=100)\n', (1469, 1485), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1551, 1572), 'sqlmodel.Field', 'Field', ([], {'max_length': '(255)'}), '(max_length=255)\n', (1556, 1572), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1668, 1689), 'sqlmodel.Field', 'Field', ([], {'max_length': '(255)'}), '(max_length=255)\n', (1673, 1689), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1775, 1818), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""users.id"""'}), "(default=None, foreign_key='users.id')\n", (1780, 1818), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1839, 1878), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""api_keys"""'}), "(back_populates='api_keys')\n", (1851, 1878), False, 'from sqlmodel import SQLModel, Field, Relationship\n'), ((1988, 2005), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (2003, 2005), False, 'from datetime import datetime\n')]