from typing import Tuple

from sqlalchemy import TIMESTAMP, Column, Integer, LargeBinary, MetaData, Table, Text, UniqueConstraint, func
from sqlalchemy_utils import IPAddressType


metadata = MetaData()


def timestamps(indexed: bool = False) -> Tuple[Column, Column]:
    return (
        Column(
            "created_at",
            TIMESTAMP(timezone=True),
            server_default=func.now(),
            nullable=False,
            index=indexed,
        ),
        Column(
            "updated_at",
            TIMESTAMP(timezone=True),
            server_default=func.now(),
            nullable=False,
            index=indexed,
        ),
    )


experiments_table = Table(
    "experiments",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("organization_name", Text, nullable=False, index=True),
    Column("model_name", Text, nullable=False, index=True),
    Column("creator", Text, nullable=False, index=True),
    Column("coordinator_ip", IPAddressType),
    Column("coordinator_port", Integer),
    Column("auth_server_public_key", LargeBinary),
    Column("auth_server_private_key", LargeBinary),
    *timestamps(),
    UniqueConstraint("organization_name", "model_name", name="uix_1"),
)
