"""Initial cart tables

Revision ID: cart_0001
Revises: None
Create Date: 2026-02-26
"""

import sqlalchemy as sa
from alembic import op

revision = "cart_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 _table_exists(op.get_bind(), "orders"):
        return

    op.create_table(
        "page_configs",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("container_type", sa.String(32), nullable=False, server_default="page"),
        sa.Column("container_id", sa.Integer, nullable=False),
        sa.Column("features", sa.JSON, nullable=False, server_default="{}"),
        sa.Column("sumup_merchant_code", sa.String(64), nullable=True),
        sa.Column("sumup_api_key", sa.Text, nullable=True),
        sa.Column("sumup_checkout_prefix", sa.String(64), 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()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )

    op.create_table(
        "cart_items",
        sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
        sa.Column("user_id", sa.Integer, nullable=True),
        sa.Column("session_id", sa.String(128), nullable=True),
        sa.Column("product_id", sa.Integer, nullable=False),
        sa.Column("quantity", sa.Integer, nullable=False, server_default="1"),
        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()),
        sa.Column("market_place_id", sa.Integer, nullable=True, index=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_cart_items_user_product", "cart_items", ["user_id", "product_id"])
    op.create_index("ix_cart_items_session_product", "cart_items", ["session_id", "product_id"])

    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, index=True),
        sa.Column("page_config_id", sa.Integer, nullable=True, index=True),
        sa.Column("status", sa.String(32), nullable=False, server_default="pending"),
        sa.Column("currency", sa.String(16), nullable=False),
        sa.Column("total_amount", sa.Numeric(12, 2), nullable=False),
        sa.Column("description", sa.Text, nullable=True, index=True),
        sa.Column("sumup_reference", sa.String(255), nullable=True, index=True),
        sa.Column("sumup_checkout_id", sa.String(128), nullable=True, index=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_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("quantity", sa.Integer, nullable=False),
        sa.Column("unit_price", sa.Numeric(12, 2), nullable=False),
        sa.Column("currency", sa.String(16), nullable=False),
        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")
    op.drop_table("cart_items")
    op.drop_table("page_configs")
