"""Add orders and order_items tables for checkout"""

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "0003_add_orders"
down_revision = "0002_add_cart_items"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "orders",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("session_id", sa.String(length=64), nullable=True),

        sa.Column(
            "status",
            sa.String(length=32),
            nullable=False,
            server_default="pending",
        ),
        sa.Column(
            "currency",
            sa.String(length=16),
            nullable=False,
            server_default="GBP",
        ),
        sa.Column(
            "total_amount",
            sa.Numeric(12, 2),
            nullable=False,
        ),

        # SumUp integration fields
        sa.Column("sumup_checkout_id", sa.String(length=128), nullable=True),
        sa.Column("sumup_status", sa.String(length=32), nullable=True),
        sa.Column("sumup_hosted_url", sa.Text(), nullable=True),

        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )

    # Indexes to match model hints (session_id + sumup_checkout_id index=True)
    op.create_index(
        "ix_orders_session_id",
        "orders",
        ["session_id"],
        unique=False,
    )
    op.create_index(
        "ix_orders_sumup_checkout_id",
        "orders",
        ["sumup_checkout_id"],
        unique=False,
    )

    op.create_table(
        "order_items",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column(
            "order_id",
            sa.Integer(),
            sa.ForeignKey("orders.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "product_id",
            sa.Integer(),
            sa.ForeignKey("products.id"),
            nullable=False,
        ),
        sa.Column("product_title", sa.String(length=512), nullable=True),

        sa.Column(
            "quantity",
            sa.Integer(),
            nullable=False,
            server_default="1",
        ),
        sa.Column(
            "unit_price",
            sa.Numeric(12, 2),
            nullable=False,
        ),
        sa.Column(
            "currency",
            sa.String(length=16),
            nullable=False,
            server_default="GBP",
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )


def downgrade() -> None:
    op.drop_table("order_items")
    op.drop_index("ix_orders_sumup_checkout_id", table_name="orders")
    op.drop_index("ix_orders_session_id", table_name="orders")
    op.drop_table("orders")
