"""add_tool_config_fields

Revision ID: 398082499c30
Revises: af8512103d67
Create Date: 2026-05-22 18:38:20.166184
"""

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

# revision identifiers, used by Alembic.
revision = '398082499c30'
down_revision = 'af8512103d67'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add new columns to tool_configs
    op.add_column('tool_configs', sa.Column('port_override', sa.Integer(), nullable=True))
    op.add_column('tool_configs', sa.Column('start_command', sa.Text(), nullable=True))
    op.add_column('tool_configs', sa.Column('working_directory', sa.Text(), nullable=True))
    op.add_column('tool_configs', sa.Column('environment_variables', postgresql.JSONB(astext_type=sa.Text()), nullable=True, server_default='{}'))
    op.add_column('tool_configs', sa.Column('volumes', postgresql.JSONB(astext_type=sa.Text()), nullable=True, server_default='[]'))
    
    # Add CHECK constraint for port range
    op.create_check_constraint('chk_port_range', 'tool_configs', sa.text('port_override IS NULL OR (port_override >= 1 AND port_override <= 65535)'))


def downgrade() -> None:
    # Drop CHECK constraint
    op.drop_constraint('chk_port_range', 'tool_configs', type_='check')
    
    # Drop columns
    op.drop_column('tool_configs', 'port_override')
    op.drop_column('tool_configs', 'start_command')
    op.drop_column('tool_configs', 'working_directory')
    op.drop_column('tool_configs', 'environment_variables')
    op.drop_column('tool_configs', 'volumes')
