"""create circuits table

Revision ID: e81f833e28fa
Revises: 5c6942374c62
Create Date: 2024-02-23 17:34:15.169451

"""

import enum

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

from ai.backend.appproxy.common.types import RouteInfo
from ai.backend.appproxy.coordinator.models.base import (
    GUID,
    StructuredJSONObjectListColumn,
)

# revision identifiers, used by Alembic.
revision = "e81f833e28fa"
down_revision = "5c6942374c62"
branch_labels = None
depends_on = None


class FrontendMode(enum.StrEnum):
    WILDCARD_DOMAIN = "wildcard"
    PORT = "port"


class ProxyProtocol(enum.StrEnum):
    HTTP = "http"
    GRPC = "grpc"
    HTTP2 = "h2"
    TCP = "tcp"


class AppMode(enum.StrEnum):
    INTERACTIVE = "interactive"
    INFERENCE = "inference"


appmode_choices = [v.name for v in AppMode]
appmode = postgresql.ENUM(*appmode_choices, name="appmode", create_type=False)

frontendmode_choices = [v.name for v in FrontendMode]
frontendmode = postgresql.ENUM(*frontendmode_choices, name="frontendmode", create_type=False)

proxyprotocol_choices = [v.name for v in ProxyProtocol]
proxyprotocol = postgresql.ENUM(*proxyprotocol_choices, name="proxyprotocol", create_type=False)


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "circuits",
        sa.Column("id", GUID(), server_default=sa.text("uuid_generate_v4()"), nullable=False),
        sa.Column("app", sa.String(length=255), nullable=True),
        sa.Column("protocol", proxyprotocol, nullable=False),
        sa.Column("worker", GUID(), nullable=False),
        sa.Column("app_mode", appmode, nullable=False),
        sa.Column("frontend_mode", frontendmode, nullable=False),
        sa.Column("port", sa.Integer(), nullable=True),
        sa.Column("subdomain", sa.String(length=255), nullable=True),
        sa.Column("envs", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("arguments", sa.String(length=1000), nullable=True),
        sa.Column("open_to_public", sa.Boolean(), nullable=False),
        sa.Column("allowed_client_ips", sa.String(length=255), nullable=True),
        sa.Column("user_id", GUID(), nullable=True),
        sa.Column("endpoint_id", GUID(), nullable=True),
        sa.Column("session_ids", sa.ARRAY(GUID()), nullable=False),
        sa.Column("route_info", StructuredJSONObjectListColumn(RouteInfo), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["worker"], ["workers.id"], name=op.f("fk_circuits_worker_workers")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_circuits")),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("circuits")
    # ### end Alembic commands ###
