"""make default_port non-nullable and set values

Revision ID: 0012_default_port_req
Revises: 0011_tool_instance_tunnel_fields
Create Date: 2026-05-20 15:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = "0012_default_port_req"
down_revision: Union[str, None] = "0011_tool_instance_tunnel_fields"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Set default_port for existing built-in tool types
    op.execute("""
        UPDATE tool_types 
        SET default_port = CASE 
            WHEN name = 'code-server' THEN 8443
            WHEN name = 'jupyter-notebook' THEN 8888
            WHEN name = 'opencode' THEN 3000
            ELSE 8080
        END
        WHERE default_port IS NULL
    """)
    
    # Make default_port non-nullable
    op.alter_column(
        "tool_types",
        "default_port",
        existing_type=sa.Integer(),
        nullable=False,
    )


def downgrade() -> None:
    op.alter_column(
        "tool_types",
        "default_port",
        existing_type=sa.Integer(),
        nullable=True,
    )
