"""Create table_combination and combined_chair_config tables.

Adds FK constraint from reservation.combination_id to table_combination.id.

Revision ID: 0006_table_combinations
Revises: 0005_table_entity_promotion
Create Date: 2026-02-24
"""

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel
from sqlalchemy.dialects import postgresql

from alembic import op

revision: str = "0006_table_combinations"
down_revision: str | None = "0005_table_entity_promotion"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ── (a) Create table_combination table ───────────────────────────────────
    op.create_table(
        "table_combination",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("table_ids", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("combined_capacity", sa.Integer(), nullable=False, server_default="0"),
        sa.ForeignKeyConstraint(["restaurant_id"], ["restaurant.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_table_combination_restaurant_id"),
        "table_combination",
        ["restaurant_id"],
        unique=False,
    )

    # ── (b) Create combined_chair_config table ───────────────────────────────
    op.create_table(
        "combined_chair_config",
        sa.Column("slot_index", sa.Integer(), nullable=False),
        sa.Column("side", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("combination_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("table_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.ForeignKeyConstraint(["combination_id"], ["table_combination.id"]),
        sa.ForeignKeyConstraint(["table_id"], ["floor_table.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_combined_chair_config_combination_id"),
        "combined_chair_config",
        ["combination_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_combined_chair_config_table_id"),
        "combined_chair_config",
        ["table_id"],
        unique=False,
    )

    # ── (c) Add FK from reservation.combination_id to table_combination ──────
    op.create_index(
        op.f("ix_reservation_combination_id"),
        "reservation",
        ["combination_id"],
        unique=False,
    )
    op.create_foreign_key(
        "fk_reservation_combination_id",
        "reservation",
        "table_combination",
        ["combination_id"],
        ["id"],
    )


def downgrade() -> None:
    # Remove reservation FK
    op.drop_constraint("fk_reservation_combination_id", "reservation", type_="foreignkey")
    op.drop_index(op.f("ix_reservation_combination_id"), table_name="reservation")

    # Drop tables in reverse dependency order
    op.drop_index(op.f("ix_combined_chair_config_table_id"), table_name="combined_chair_config")
    op.drop_index(
        op.f("ix_combined_chair_config_combination_id"), table_name="combined_chair_config"
    )
    op.drop_table("combined_chair_config")
    op.drop_index(op.f("ix_table_combination_restaurant_id"), table_name="table_combination")
    op.drop_table("table_combination")
