"""Add scheduling history tables

Revision ID: b6a20822c683
Revises: b0fb0eb6b6bc
Create Date: 2025-12-13 23:35:12.148775

"""

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

from ai.backend.manager.models.base import GUID

# revision identifiers, used by Alembic.
revision = "b6a20822c683"
down_revision = "b0fb0eb6b6bc"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "deployment_history",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("deployment_id", GUID(), nullable=False),
        sa.Column("phase", sa.String(length=64), nullable=False),
        sa.Column("from_status", sa.String(length=64), nullable=True),
        sa.Column("to_status", sa.String(length=64), nullable=True),
        sa.Column("result", sa.String(length=32), nullable=False),
        sa.Column("error_code", sa.String(length=128), nullable=True),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("attempts", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_deployment_history")),
    )
    op.create_index(
        op.f("ix_deployment_history_deployment_id"),
        "deployment_history",
        ["deployment_id"],
        unique=False,
    )
    op.create_table(
        "kernel_scheduling_history",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("kernel_id", GUID(), nullable=False),
        sa.Column("session_id", GUID(), nullable=False),
        sa.Column("phase", sa.String(length=64), nullable=False),
        sa.Column("from_status", sa.String(length=64), nullable=True),
        sa.Column("to_status", sa.String(length=64), nullable=True),
        sa.Column("result", sa.String(length=32), nullable=False),
        sa.Column("error_code", sa.String(length=128), nullable=True),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("attempts", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_kernel_scheduling_history")),
    )
    op.create_index(
        op.f("ix_kernel_scheduling_history_kernel_id"),
        "kernel_scheduling_history",
        ["kernel_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_kernel_scheduling_history_session_id"),
        "kernel_scheduling_history",
        ["session_id"],
        unique=False,
    )
    op.create_table(
        "route_history",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("route_id", GUID(), nullable=False),
        sa.Column("deployment_id", GUID(), nullable=False),
        sa.Column("phase", sa.String(length=64), nullable=False),
        sa.Column("from_status", sa.String(length=64), nullable=True),
        sa.Column("to_status", sa.String(length=64), nullable=True),
        sa.Column("result", sa.String(length=32), nullable=False),
        sa.Column("error_code", sa.String(length=128), nullable=True),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("attempts", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_route_history")),
    )
    op.create_index(
        op.f("ix_route_history_deployment_id"), "route_history", ["deployment_id"], unique=False
    )
    op.create_index(op.f("ix_route_history_route_id"), "route_history", ["route_id"], unique=False)
    op.create_table(
        "session_scheduling_history",
        sa.Column(
            "id",
            GUID(),
            server_default=sa.text("uuid_generate_v4()"),
            nullable=False,
        ),
        sa.Column("session_id", GUID(), nullable=False),
        sa.Column("phase", sa.String(length=64), nullable=False),
        sa.Column("from_status", sa.String(length=64), nullable=True),
        sa.Column("to_status", sa.String(length=64), nullable=True),
        sa.Column("result", sa.String(length=32), nullable=False),
        sa.Column("error_code", sa.String(length=128), nullable=True),
        sa.Column("message", sa.Text(), nullable=False),
        sa.Column("sub_steps", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("attempts", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_session_scheduling_history")),
    )
    op.create_index(
        op.f("ix_session_scheduling_history_session_id"),
        "session_scheduling_history",
        ["session_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_session_scheduling_history_session_id"), table_name="session_scheduling_history"
    )
    op.drop_table("session_scheduling_history")
    op.drop_index(op.f("ix_route_history_route_id"), table_name="route_history")
    op.drop_index(op.f("ix_route_history_deployment_id"), table_name="route_history")
    op.drop_table("route_history")
    op.drop_index(
        op.f("ix_kernel_scheduling_history_session_id"), table_name="kernel_scheduling_history"
    )
    op.drop_index(
        op.f("ix_kernel_scheduling_history_kernel_id"), table_name="kernel_scheduling_history"
    )
    op.drop_table("kernel_scheduling_history")
    op.drop_index(op.f("ix_deployment_history_deployment_id"), table_name="deployment_history")
    op.drop_table("deployment_history")
    # ### end Alembic commands ###
