"""Remove customer review relationship

Revision ID: 225a11d65bed
Revises: 7e09f220937b
Create Date: 2026-09-29 08:08:55.799156

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql


# revision identifiers, used by Alembic.
revision = "225a11d65bed"
down_revision = "7e09f220937b"
branch_labels = None
depends_on = None


def upgrade():

    # Remove the old customer relationship from carts.
    with op.batch_alter_table("carts", schema=None) as batch_op:

        batch_op.drop_constraint(
            "carts_ibfk_1",
            type_="foreignkey"
        )

        batch_op.drop_column(
            "customer_id"
        )


    # Remove the old customer relationship from orders.
    with op.batch_alter_table("orders", schema=None) as batch_op:

        batch_op.drop_constraint(
            "orders_ibfk_1",
            type_="foreignkey"
        )

        batch_op.drop_column(
            "customer_id"
        )


    # Replace the old customer relationship on reviews
    # with the guest reviewer's name.
    with op.batch_alter_table("reviews", schema=None) as batch_op:

        batch_op.add_column(
            sa.Column(
                "reviewer_name",
                sa.String(length=150),
                nullable=False
            )
        )

        batch_op.drop_constraint(
            "reviews_ibfk_1",
            type_="foreignkey"
        )

        batch_op.drop_column(
            "customer_id"
        )


    # Remove old customer-related tables.
    op.drop_table("addresses")

    op.drop_table("notifications")

    op.drop_table("customers")


def downgrade():

    # Recreate customers first because the other tables
    # depend on it.
    op.create_table(
        "customers",

        sa.Column(
            "id",
            mysql.INTEGER(display_width=11),
            autoincrement=True,
            nullable=False
        ),

        sa.Column(
            "first_name",
            mysql.VARCHAR(length=100),
            nullable=False
        ),

        sa.Column(
            "last_name",
            mysql.VARCHAR(length=100),
            nullable=False
        ),

        sa.Column(
            "phone",
            mysql.VARCHAR(length=30),
            nullable=False
        ),

        sa.Column(
            "email",
            mysql.VARCHAR(length=150),
            nullable=True
        ),

        sa.Column(
            "password_hash",
            mysql.VARCHAR(length=255),
            nullable=True
        ),

        sa.Column(
            "is_active",
            mysql.TINYINT(display_width=1),
            autoincrement=False,
            nullable=False
        ),

        sa.Column(
            "created_at",
            mysql.DATETIME(),
            nullable=False
        ),

        sa.Column(
            "updated_at",
            mysql.DATETIME(),
            nullable=False
        ),

        sa.PrimaryKeyConstraint("id"),

        mysql_collate="latin1_swedish_ci",
        mysql_default_charset="latin1",
        mysql_engine="InnoDB"
    )


    # Recreate customer_id on carts.
    with op.batch_alter_table("carts", schema=None) as batch_op:

        batch_op.add_column(
            sa.Column(
                "customer_id",
                mysql.INTEGER(display_width=11),
                autoincrement=False,
                nullable=True
            )
        )

        batch_op.create_foreign_key(
            "carts_ibfk_1",
            "customers",
            ["customer_id"],
            ["id"]
        )


    # Recreate customer_id on orders.
    with op.batch_alter_table("orders", schema=None) as batch_op:

        batch_op.add_column(
            sa.Column(
                "customer_id",
                mysql.INTEGER(display_width=11),
                autoincrement=False,
                nullable=True
            )
        )

        batch_op.create_foreign_key(
            "orders_ibfk_1",
            "customers",
            ["customer_id"],
            ["id"]
        )


    # Recreate customer_id on reviews.
    with op.batch_alter_table("reviews", schema=None) as batch_op:

        batch_op.add_column(
            sa.Column(
                "customer_id",
                mysql.INTEGER(display_width=11),
                autoincrement=False,
                nullable=True
            )
        )

        batch_op.create_foreign_key(
            "reviews_ibfk_1",
            "customers",
            ["customer_id"],
            ["id"]
        )

        batch_op.drop_column(
            "reviewer_name"
        )


    # Recreate addresses.
    op.create_table(
        "addresses",

        sa.Column(
            "id",
            mysql.INTEGER(display_width=11),
            autoincrement=True,
            nullable=False
        ),

        sa.Column(
            "customer_id",
            mysql.INTEGER(display_width=11),
            autoincrement=False,
            nullable=False
        ),

        sa.Column(
            "name",
            mysql.VARCHAR(length=150),
            nullable=False
        ),

        sa.Column(
            "phone",
            mysql.VARCHAR(length=30),
            nullable=False
        ),

        sa.Column(
            "address_line",
            mysql.TEXT(),
            nullable=False
        ),

        sa.Column(
            "city",
            mysql.VARCHAR(length=100),
            nullable=True
        ),

        sa.Column(
            "district",
            mysql.VARCHAR(length=100),
            nullable=True
        ),

        sa.Column(
            "delivery_notes",
            mysql.TEXT(),
            nullable=True
        ),

        sa.Column(
            "is_default",
            mysql.TINYINT(display_width=1),
            autoincrement=False,
            nullable=False
        ),

        sa.Column(
            "created_at",
            mysql.DATETIME(),
            nullable=False
        ),

        sa.Column(
            "updated_at",
            mysql.DATETIME(),
            nullable=False
        ),

        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name="addresses_ibfk_1"
        ),

        sa.PrimaryKeyConstraint("id"),

        mysql_collate="latin1_swedish_ci",
        mysql_default_charset="latin1",
        mysql_engine="InnoDB"
    )


    # Recreate notifications.
    op.create_table(
        "notifications",

        sa.Column(
            "id",
            mysql.INTEGER(display_width=11),
            autoincrement=True,
            nullable=False
        ),

        sa.Column(
            "customer_id",
            mysql.INTEGER(display_width=11),
            autoincrement=False,
            nullable=False
        ),

        sa.Column(
            "order_id",
            mysql.INTEGER(display_width=11),
            autoincrement=False,
            nullable=True
        ),

        sa.Column(
            "type",
            mysql.VARCHAR(length=50),
            nullable=False
        ),

        sa.Column(
            "message",
            mysql.TEXT(),
            nullable=False
        ),

        sa.Column(
            "is_read",
            mysql.TINYINT(display_width=1),
            autoincrement=False,
            nullable=False
        ),

        sa.Column(
            "created_at",
            mysql.DATETIME(),
            nullable=False
        ),

        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name="notifications_ibfk_1"
        ),

        sa.ForeignKeyConstraint(
            ["order_id"],
            ["orders.id"],
            name="notifications_ibfk_2"
        ),

        sa.PrimaryKeyConstraint("id"),

        mysql_collate="latin1_swedish_ci",
        mysql_default_charset="latin1",
        mysql_engine="InnoDB"
    )