"""Remove broken command override from LSIO code-server templates

Revision ID: 2026_05_29_remove_lsio_command_override
Revises: 2026_05_29_fix_code_server_bind_addr
Create Date: 2026-05-29 15:05:00.000000

"""

from collections.abc import Sequence

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = "2026_05_29_remove_lsio_command_override"
down_revision: str | None = "2026_05_29_fix_code_server_bind_addr"
branch_labels: Sequence[str] | None = None
depends_on: Sequence[str] | None = None


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

    # Fix tool_types templates in DB
    result = conn.execute(
        sa.text("""
            SELECT id, compose_template
            FROM tool_types
            WHERE name = 'code-server'
        """)
    ).fetchall()

    import yaml
    from pathlib import Path

    for tool_id, compose_template in result:
        try:
            data = yaml.safe_load(compose_template)
        except Exception:
            continue

        if not data or "services" not in data:
            continue

        modified = False
        for svc in data["services"].values():
            image = svc.get("image", "")
            if not image or "linuxserver" not in image:
                continue
            if "command" in svc:
                cmd = svc["command"]
                if "--bind-addr" in cmd or "--host" in cmd:
                    del svc["command"]
                    modified = True

        if modified:
            updated = yaml.dump(data, default_flow_style=False)
            conn.execute(
                sa.text("""
                    UPDATE tool_types
                    SET compose_template = :compose_template
                    WHERE id = :id
                """),
                {"compose_template": updated, "id": tool_id},
            )
            print(f"Removed broken command override from LSIO template ({tool_id})")

    # Fix existing instance compose files on disk
    # Use information_schema to check if compose_path column exists
    col_result = conn.execute(
        sa.text("""
            SELECT column_name
            FROM information_schema.columns
            WHERE table_name = 'tool_instances'
            AND column_name = 'compose_path'
        """)
    ).fetchone()

    if col_result:
        result = conn.execute(
            sa.text("""
                SELECT id, compose_path
                FROM tool_instances
                WHERE compose_path IS NOT NULL
            """)
        ).fetchall()

        for instance_id, compose_path in result:
            path = Path(compose_path)
            if not path.exists():
                continue
            try:
                content = path.read_text()
                data = yaml.safe_load(content)
            except Exception:
                continue

            if not data or "services" not in data:
                continue

            modified = False
            for svc in data["services"].values():
                image = svc.get("image", "")
                if not image or "linuxserver" not in image:
                    continue
                if "command" in svc:
                    cmd = svc["command"]
                    if "--bind-addr" in cmd or "--host" in cmd:
                        del svc["command"]
                        modified = True

            if modified:
                path.write_text(yaml.dump(data, default_flow_style=False))
                print(
                    f"Removed broken command override from instance compose "
                    f"({instance_id})"
                )


def downgrade() -> None:
    pass
