"""Add gateway_id foreign key to agents table.

Agents need to belong to a gateway so that main-agent provisioning
and session lookups can resolve which gateway an agent connects to.

Revision ID: d2a3b4c5e6f7
Revises: c4a1d2e3f4a5
Create Date: 2026-05-21 04:30:00.000000

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
revision = "d2a3b4c5e6f7"
down_revision = "c4a1d2e3f4a5"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Add gateway_id column and foreign key to agents."""
    op.add_column(
        "agents",
        sa.Column("gateway_id", sa.Uuid(), nullable=True),
    )
    op.create_index("ix_agents_gateway_id", "agents", ["gateway_id"], unique=False)
    op.create_foreign_key("agents_gateway_id_fkey", "agents", "gateways", ["gateway_id"], ["id"])


def downgrade() -> None:
    """Remove gateway_id from agents."""
    op.drop_constraint("agents_gateway_id_fkey", "agents", type_="foreignkey")
    op.drop_index("ix_agents_gateway_id", table_name="agents")
    op.drop_column("agents", "gateway_id")