"""Checkpoint B: profils de locaux et compositions facultatives."""
from alembic import op
import sqlalchemy as sa

revision = "k6f7a8b9c0d1"
down_revision = "j5e6f7a8b9c0"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "room_profiles",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("name", sa.String(120), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("room_type_id", sa.Integer(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["room_type_id"], ["room_types.id"]),
        sa.UniqueConstraint("name", name="uq_room_profiles_name"),
    )
    op.create_index("ix_room_profiles_room_type_id", "room_profiles", ["room_type_id"])
    op.create_table(
        "room_profile_items",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("profile_id", sa.Integer(), nullable=False),
        sa.Column("label", sa.String(200), nullable=False),
        sa.Column("category_id", sa.Integer(), nullable=True),
        sa.Column("lot_id", sa.Integer(), nullable=True),
        sa.Column("quantity", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("is_group", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("enabled_by_default", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
        sa.ForeignKeyConstraint(["profile_id"], ["room_profiles.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["category_id"], ["equipment_categories.id"]),
        sa.ForeignKeyConstraint(["lot_id"], ["lots.id"]),
    )
    op.create_index("ix_room_profile_items_profile_id", "room_profile_items", ["profile_id"])
    op.create_index("ix_room_profile_items_category_id", "room_profile_items", ["category_id"])
    op.create_index("ix_room_profile_items_lot_id", "room_profile_items", ["lot_id"])
    op.create_table(
        "room_surfaces",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("room_id", sa.Integer(), nullable=True),
        sa.Column("profile_id", sa.Integer(), nullable=True),
        sa.Column("surface_type", sa.String(30), nullable=False),
        sa.Column("material", sa.String(80), nullable=False),
        sa.Column("finish", sa.String(80), nullable=True),
        sa.Column("label", sa.String(120), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
        sa.ForeignKeyConstraint(["room_id"], ["rooms.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["profile_id"], ["room_profiles.id"], ondelete="CASCADE"),
    )
    op.create_index("ix_room_surfaces_room_id", "room_surfaces", ["room_id"])
    op.create_index("ix_room_surfaces_profile_id", "room_surfaces", ["profile_id"])


def downgrade():
    op.drop_index("ix_room_surfaces_profile_id", table_name="room_surfaces")
    op.drop_index("ix_room_surfaces_room_id", table_name="room_surfaces")
    op.drop_table("room_surfaces")
    op.drop_index("ix_room_profile_items_lot_id", table_name="room_profile_items")
    op.drop_index("ix_room_profile_items_category_id", table_name="room_profile_items")
    op.drop_index("ix_room_profile_items_profile_id", table_name="room_profile_items")
    op.drop_table("room_profile_items")
    op.drop_index("ix_room_profiles_room_type_id", table_name="room_profiles")
    op.drop_table("room_profiles")
