from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR
from pydantic import BaseModel
from datetime import datetime
from typing import List, Optional
from sqlalchemy import String, Column


class ServerCatagoryLink(SQLModel, table=True):
    server_id: Optional[int] = Field(
        default=None, foreign_key="server.id", primary_key=True
    )
    catagory_id: Optional[int] = Field(
        default=None, foreign_key="catagory.id", primary_key=True
    )


class ServerOrganizationLink(SQLModel, table=True):
    server_id: Optional[int] = Field(
        default=None, foreign_key="server.id", primary_key=True
    )
    organization_id: Optional[int] = Field(
        default=None, foreign_key="organization.id", primary_key=True
    )


class Catagory(SQLModel, table=True):
    id: int = Field(primary_key=True)
    title: str = Field(sa_column=Column("title", String(255), unique=True))
    meta_ref: str = Field(sa_column=Column("meta_ref", String(255), unique=True))
    color: str
    servers: List["Server"] = Relationship(
        back_populates="catagories", link_model=ServerCatagoryLink
    )


class SaveCatagory(SQLModel):
    title: str
    color: str


class Organization(SQLModel, table=True):
    id: int = Field(primary_key=True)
    title: str = Field(sa_column=Column("title", String(255), unique=True))
    parent_id: Optional[int] = Field(foreign_key="organization.id")
    ref_title: str
    description: str
    servers: List["Server"] = Relationship(
        back_populates="organizations", link_model=ServerOrganizationLink
    )


class Server(SQLModel, table=True):
    id: int = Field(primary_key=True)
    scheme: str = Field(default="http")
    domain_name: str = Field(sa_column=Column("domain_name", String(255), unique=True))
    path: Optional[str]
    agency: Optional[int]
    organization: Optional[str]
    status: str = "LOADING"
    server_log: List["ServerLog"] = Relationship(back_populates="server")
    server_reports: List["ServerReport"] = Relationship(back_populates="server")
    clicks: int = 0
    ipaddress: Optional[str]
    response_time: Optional[int] = None
    last_checked: Optional[datetime]
    catagories: List["Catagory"] = Relationship(
        back_populates="servers", link_model=ServerCatagoryLink
    )
    organizations: List["Organization"] = Relationship(
        back_populates="servers", link_model=ServerOrganizationLink
    )

    class Config:
        arbitrary_types_allowed = True


class ServerLog(SQLModel, table=True):
    id: int = Field(primary_key=True)
    datetime: datetime
    server_id: Optional[int] = Field(default=None, foreign_key="server.id")
    server: Optional[Server] = Relationship(back_populates="server_log")
    response_code: Optional[int]
    response_time: Optional[int]
    ipaddress: Optional[str]
    url: str

    error: Optional[str]


class ServerReport(SQLModel, table=True):
    id: int = Field(primary_key=True)
    datetime: datetime
    server_id: Optional[int] = Field(default=None, foreign_key="server.id")
    server: Optional[Server] = Relationship(back_populates="server_reports")

sqlmodel.Field sqlalchemy.String sqlmodel.Relationship
[((280, 342), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""server.id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='server.id', primary_key=True)\n", (285, 342), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((390, 454), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""catagory.id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='catagory.id', primary_key=True)\n", (395, 454), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((554, 616), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""server.id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='server.id', primary_key=True)\n", (559, 616), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((668, 736), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""organization.id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='organization.id', primary_key=True)\n", (673, 736), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((805, 828), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (810, 828), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1032, 1104), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""catagories"""', 'link_model': 'ServerCatagoryLink'}), "(back_populates='catagories', link_model=ServerCatagoryLink)\n", (1044, 1104), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1239, 1262), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (1244, 1262), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1370, 1406), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""organization.id"""'}), "(foreign_key='organization.id')\n", (1375, 1406), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1477, 1556), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""organizations"""', 'link_model': 'ServerOrganizationLink'}), "(back_populates='organizations', link_model=ServerOrganizationLink)\n", (1489, 1556), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1623, 1646), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (1628, 1646), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1665, 1686), 'sqlmodel.Field', 'Field', ([], {'default': '"""http"""'}), "(default='http')\n", (1670, 1686), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((1921, 1958), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""server"""'}), "(back_populates='server')\n", (1933, 1958), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2002, 2039), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""server"""'}), "(back_populates='server')\n", (2014, 2039), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2201, 2270), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""servers"""', 'link_model': 'ServerCatagoryLink'}), "(back_populates='servers', link_model=ServerCatagoryLink)\n", (2213, 2270), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2327, 2400), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""servers"""', 'link_model': 'ServerOrganizationLink'}), "(back_populates='servers', link_model=ServerOrganizationLink)\n", (2339, 2400), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2528, 2551), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (2533, 2551), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2606, 2650), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""server.id"""'}), "(default=None, foreign_key='server.id')\n", (2611, 2650), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2682, 2723), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""server_log"""'}), "(back_populates='server_log')\n", (2694, 2723), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2916, 2939), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (2921, 2939), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((2994, 3038), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""server.id"""'}), "(default=None, foreign_key='server.id')\n", (2999, 3038), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((3070, 3115), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""server_reports"""'}), "(back_populates='server_reports')\n", (3082, 3115), False, 'from sqlmodel import SQLModel, Field, JSON, Relationship, VARCHAR\n'), ((878, 889), 'sqlalchemy.String', 'String', (['(255)'], {}), '(255)\n', (884, 889), False, 'from sqlalchemy import String, Column\n'), ((960, 971), 'sqlalchemy.String', 'String', (['(255)'], {}), '(255)\n', (966, 971), False, 'from sqlalchemy import String, Column\n'), ((1312, 1323), 'sqlalchemy.String', 'String', (['(255)'], {}), '(255)\n', (1318, 1323), False, 'from sqlalchemy import String, Column\n'), ((1748, 1759), 'sqlalchemy.String', 'String', (['(255)'], {}), '(255)\n', (1754, 1759), False, 'from sqlalchemy import String, Column\n')]