"""drop tool_configs and config_folders tables

Revision ID: 2026_05_28_drop_tool_configs_and_config_folders
Revises: 2026_05_28_add_tool_definition_manifests
Create Date: 2026-05-28

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = "2026_05_28_drop_tool_configs_and_config_folders"
down_revision: Union[str, None] = "2026_05_28_add_terminal_sessions"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    conn = op.get_bind()

    # Drop tool_configs table if it exists
    result = conn.execute(
        sa.text("""
            SELECT table_name FROM information_schema.tables
            WHERE table_name = 'tool_configs'
        """)
    )
    if result.fetchone():
        op.drop_table("tool_configs")

    # Drop config_folders table if it exists
    result = conn.execute(
        sa.text("""
            SELECT table_name FROM information_schema.tables
            WHERE table_name = 'config_folders'
        """)
    )
    if result.fetchone():
        op.drop_table("config_folders")


def downgrade() -> None:
    # Recreate config_folders table
    op.create_table(
        "config_folders",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("mount_path", sa.String(1024), nullable=False),
        sa.Column("files", sa.JSON(), default=dict, nullable=False),
        sa.Column("project_overrides", sa.JSON(), default=dict, nullable=True),
        sa.Column("is_active", sa.Boolean(), default=True, nullable=False),
        sa.Column(
            "created_at", sa.TIMESTAMP(timezone=True), server_default=sa.func.now()
        ),
        sa.Column(
            "updated_at", sa.TIMESTAMP(timezone=True), server_default=sa.func.now()
        ),
        sa.PrimaryKeyConstraint("id"),
    )

    # Recreate tool_configs table
    op.create_table(
        "tool_configs",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("tool_type_id", sa.UUID(), nullable=False),
        sa.Column("project_id", sa.UUID(), nullable=True),
        sa.Column("key", sa.String(255), nullable=False),
        sa.Column("value", sa.Text(), nullable=False),
        sa.Column("config_type", sa.String(20), default="env", nullable=False),
        sa.Column("file_path", sa.String(1024), nullable=True),
        sa.Column("port_override", sa.Integer(), nullable=True),
        sa.Column("start_command", sa.Text(), nullable=True),
        sa.Column("working_directory", sa.Text(), nullable=True),
        sa.Column("environment_variables", sa.JSON(), default=dict, nullable=True),
        sa.Column("volumes", sa.JSON(), default=list, nullable=True),
        sa.Column(
            "created_at", sa.TIMESTAMP(timezone=True), server_default=sa.func.now()
        ),
        sa.Column(
            "updated_at", sa.TIMESTAMP(timezone=True), server_default=sa.func.now()
        ),
        sa.PrimaryKeyConstraint("id"),
    )
