"""Initial orders tables

Revision ID: orders_0001
Revises: None
Create Date: 2026-02-27
"""

import sqlalchemy as sa
from alembic import op

revision = "orders_0001"
down_revision = None
branch_labels = None
depends_on = None


def _table_exists(conn, name):
    result = conn.execute(sa.text(
        "SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name=:t"
    ), {"t": name})
    return result.scalar() is not None


def upgrade():
    if not _table_exists(op.get_bind(), "orders"):
        op.create_table(
            "orders",
            sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
            sa.Column("user_id", sa.Integer, nullable=True),
            sa.Column("session_id", sa.String(64), nullable=True),
            sa.Column("page_config_id", sa.Integer, nullable=True),
            sa.Column("status", sa.String(32), nullable=False, server_default="pending"),
            sa.Column("currency", sa.String(16), nullable=False, server_default="GBP"),
            sa.Column("total_amount", sa.Numeric(12, 2), nullable=False),
            sa.Column("description", sa.Text, nullable=True),
            sa.Column("sumup_reference", sa.String(255), nullable=True),
            sa.Column("sumup_checkout_id", sa.String(128), nullable=True),
            sa.Column("sumup_status", sa.String(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()),
        )
        op.create_index("ix_orders_session_id", "orders", ["session_id"])
        op.create_index("ix_orders_page_config_id", "orders", ["page_config_id"])
        op.create_index("ix_orders_description", "orders", ["description"], postgresql_using="hash")
        op.create_index("ix_orders_sumup_reference", "orders", ["sumup_reference"])
        op.create_index("ix_orders_sumup_checkout_id", "orders", ["sumup_checkout_id"])

    if not _table_exists(op.get_bind(), "order_items"):
        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, nullable=False),
            sa.Column("product_title", sa.String(512), nullable=True),
            sa.Column("product_slug", sa.String(512), nullable=True),
            sa.Column("product_image", sa.Text, 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(16), nullable=False, server_default="GBP"),
            sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        )


def downgrade():
    op.drop_table("order_items")
    op.drop_table("orders")
