from datetime import datetime
from typing import TYPE_CHECKING, Optional
from models.base import BaseModel, BaseTableFields
from sqlmodel import Field, Relationship, Column, String

if TYPE_CHECKING:
    from models.user import User


class Session(BaseModel, BaseTableFields, table=True):
    __tablename__ = "sessions"

    access_token: str = Field(
        sa_column=Column(
            String,
            unique=True,
            nullable=False
        )
    )
    ip_address: Optional[str] = Field(
        sa_column=Column(
            String(100),
            nullable=True
        )
    )
    user_agent: Optional[str] = Field(
        sa_column=Column(
            String(100),
            nullable=True
        )
    )

    user_id: int = Field(foreign_key="users.id")
    user: "User" = Relationship(back_populates="sessions")


class SessionRead(BaseModel):
    id: int
    access_token: str
    ip_address: Optional[str]
    user_agent: Optional[str]
    created_at: datetime
    updated_at: datetime
    is_active: bool


class SessionCreate(BaseModel):
    access_token: str
    ip_address: Optional[str]
    user_agent: Optional[str]
    user_id: int


class SessionUpdate(BaseModel):
    is_active: Optional[bool] = None

sqlmodel.String sqlmodel.Relationship sqlmodel.Field sqlmodel.Column
[((751, 780), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""users.id"""'}), "(foreign_key='users.id')\n", (756, 780), False, 'from sqlmodel import Field, Relationship, Column, String\n'), ((800, 839), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""sessions"""'}), "(back_populates='sessions')\n", (812, 839), False, 'from sqlmodel import Field, Relationship, Column, String\n'), ((371, 414), 'sqlmodel.Column', 'Column', (['String'], {'unique': '(True)', 'nullable': '(False)'}), '(String, unique=True, nullable=False)\n', (377, 414), False, 'from sqlmodel import Field, Relationship, Column, String\n'), ((544, 555), 'sqlmodel.String', 'String', (['(100)'], {}), '(100)\n', (550, 555), False, 'from sqlmodel import Field, Relationship, Column, String\n'), ((676, 687), 'sqlmodel.String', 'String', (['(100)'], {}), '(100)\n', (682, 687), False, 'from sqlmodel import Field, Relationship, Column, String\n')]