"""Initial market tables

Revision ID: market_0001
Revises: -
Create Date: 2026-02-26
"""

import sqlalchemy as sa
from alembic import op

revision = "market_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(), "products"):
        return

    # 1. market_places
    op.create_table(
        "market_places",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("container_type", sa.String(32), nullable=False, server_default="'page'"),
        sa.Column("container_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("slug", sa.String(255), nullable=False),
        sa.Column("description", 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()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_market_places_container", "market_places", ["container_type", "container_id"])
    op.execute(
        "CREATE UNIQUE INDEX IF NOT EXISTS ux_market_places_slug_active "
        "ON market_places (LOWER(slug)) WHERE deleted_at IS NULL"
    )

    # 2. products
    op.create_table(
        "products",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("slug", sa.String(255), nullable=False, unique=True, index=True),
        sa.Column("title", sa.String(512), nullable=True),
        sa.Column("image", sa.Text(), nullable=True),
        sa.Column("description_short", sa.Text(), nullable=True),
        sa.Column("description_html", sa.Text(), nullable=True),
        sa.Column("suma_href", sa.Text(), nullable=True),
        sa.Column("brand", sa.String(255), nullable=True),
        sa.Column("rrp", sa.Numeric(12, 2), nullable=True),
        sa.Column("rrp_currency", sa.String(16), nullable=True),
        sa.Column("rrp_raw", sa.String(128), nullable=True),
        sa.Column("price_per_unit", sa.Numeric(12, 4), nullable=True),
        sa.Column("price_per_unit_currency", sa.String(16), nullable=True),
        sa.Column("price_per_unit_raw", sa.String(128), nullable=True),
        sa.Column("special_price", sa.Numeric(12, 2), nullable=True),
        sa.Column("special_price_currency", sa.String(16), nullable=True),
        sa.Column("special_price_raw", sa.String(128), nullable=True),
        sa.Column("regular_price", sa.Numeric(12, 2), nullable=True),
        sa.Column("regular_price_currency", sa.String(16), nullable=True),
        sa.Column("regular_price_raw", sa.String(128), nullable=True),
        sa.Column("oe_list_price", sa.Numeric(12, 2), nullable=True),
        sa.Column("case_size_count", sa.Integer(), nullable=True),
        sa.Column("case_size_item_qty", sa.Numeric(12, 3), nullable=True),
        sa.Column("case_size_item_unit", sa.String(32), nullable=True),
        sa.Column("case_size_raw", sa.String(128), 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),
        sa.Column("ean", sa.String(64), nullable=True),
        sa.Column("sku", sa.String(128), nullable=True),
        sa.Column("unit_size", sa.String(128), nullable=True),
        sa.Column("pack_size", sa.String(128), nullable=True),
    )

    # 3. product_images
    op.create_table(
        "product_images",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("url", sa.Text(), nullable=False),
        sa.Column("position", sa.Integer(), nullable=False),
        sa.Column("kind", sa.String(16), nullable=False),
        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),
        sa.UniqueConstraint("product_id", "url", "kind", name="uq_product_images_product_url_kind"),
    )
    op.create_index("ix_product_images_position", "product_images", ["position"])

    # 4. product_sections
    op.create_table(
        "product_sections",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("html", sa.Text(), nullable=False),
        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),
        sa.UniqueConstraint("product_id", "title", name="uq_product_sections_product_title"),
    )

    # 5. product_labels
    op.create_table(
        "product_labels",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("name", sa.String(255), nullable=False),
        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),
        sa.UniqueConstraint("product_id", "name", name="uq_product_labels_product_name"),
    )

    # 6. product_stickers
    op.create_table(
        "product_stickers",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("name", sa.String(255), nullable=False),
        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),
        sa.UniqueConstraint("product_id", "name", name="uq_product_stickers_product_name"),
    )

    # 7. product_attributes
    op.create_table(
        "product_attributes",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("key", sa.String(255), nullable=False),
        sa.Column("value", 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()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.UniqueConstraint("product_id", "key", name="uq_product_attributes_product_key"),
    )

    # 8. product_nutrition
    op.create_table(
        "product_nutrition",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("key", sa.String(255), nullable=False),
        sa.Column("value", sa.String(255), nullable=True),
        sa.Column("unit", 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),
        sa.UniqueConstraint("product_id", "key", name="uq_product_nutrition_product_key"),
    )

    # 9. product_allergens
    op.create_table(
        "product_allergens",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("contains", sa.Boolean(), nullable=False, server_default="false"),
        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),
        sa.UniqueConstraint("product_id", "name", name="uq_product_allergens_product_name"),
    )

    # 10. product_likes
    op.create_table(
        "product_likes",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("product_slug", sa.String(255), sa.ForeignKey("products.slug", ondelete="CASCADE"), 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),
    )

    # 11. nav_tops
    op.create_table(
        "nav_tops",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("label", sa.String(255), nullable=False),
        sa.Column("slug", sa.String(255), nullable=False, index=True),
        sa.Column("market_id", sa.Integer(), sa.ForeignKey("market_places.id", ondelete="SET NULL"), nullable=True, index=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),
        sa.UniqueConstraint("label", "slug", name="uq_nav_tops_label_slug"),
    )

    # 12. nav_subs
    op.create_table(
        "nav_subs",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("top_id", sa.Integer(), sa.ForeignKey("nav_tops.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("label", sa.String(255), nullable=True),
        sa.Column("slug", sa.String(255), nullable=False, index=True),
        sa.Column("href", 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()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.UniqueConstraint("top_id", "slug", name="uq_nav_subs_top_slug"),
    )

    # 13. listings
    op.create_table(
        "listings",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("top_id", sa.Integer(), sa.ForeignKey("nav_tops.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("sub_id", sa.Integer(), sa.ForeignKey("nav_subs.id", ondelete="CASCADE"), nullable=True, index=True),
        sa.Column("total_pages", sa.Integer(), 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),
        sa.UniqueConstraint("top_id", "sub_id", name="uq_listings_top_sub"),
    )

    # 14. listing_items
    op.create_table(
        "listing_items",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("listing_id", sa.Integer(), sa.ForeignKey("listings.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("slug", sa.String(255), nullable=False, index=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),
        sa.UniqueConstraint("listing_id", "slug", name="uq_listing_items_listing_slug"),
    )

    # 15. link_errors
    op.create_table(
        "link_errors",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_slug", sa.String(255), nullable=True, index=True),
        sa.Column("href", sa.Text(), nullable=True),
        sa.Column("text", sa.Text(), nullable=True),
        sa.Column("top", sa.String(255), nullable=True),
        sa.Column("sub", sa.String(255), nullable=True),
        sa.Column("target_slug", sa.String(255), nullable=True),
        sa.Column("type", sa.String(255), 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),
    )

    # 16. link_externals
    op.create_table(
        "link_externals",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("product_slug", sa.String(255), nullable=True, index=True),
        sa.Column("href", sa.Text(), nullable=True),
        sa.Column("text", sa.Text(), nullable=True),
        sa.Column("host", sa.String(255), 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),
    )

    # 17. subcategory_redirects
    op.create_table(
        "subcategory_redirects",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("old_path", sa.String(512), nullable=False, index=True),
        sa.Column("new_path", sa.String(512), nullable=False),
        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),
    )

    # 18. product_logs
    op.create_table(
        "product_logs",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("slug", sa.String(255), nullable=True, index=True),
        sa.Column("href_tried", sa.Text(), nullable=True),
        sa.Column("ok", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("error_type", sa.String(255), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("http_status", sa.Integer(), nullable=True),
        sa.Column("final_url", sa.Text(), nullable=True),
        sa.Column("transport_error", sa.Boolean(), nullable=True),
        sa.Column("title", sa.String(512), nullable=True),
        sa.Column("has_description_html", sa.Boolean(), nullable=True),
        sa.Column("has_description_short", sa.Boolean(), nullable=True),
        sa.Column("sections_count", sa.Integer(), nullable=True),
        sa.Column("images_count", sa.Integer(), nullable=True),
        sa.Column("embedded_images_count", sa.Integer(), nullable=True),
        sa.Column("all_images_count", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )


def downgrade():
    op.drop_table("product_logs")
    op.drop_table("subcategory_redirects")
    op.drop_table("link_externals")
    op.drop_table("link_errors")
    op.drop_table("listing_items")
    op.drop_table("listings")
    op.drop_table("nav_subs")
    op.drop_table("nav_tops")
    op.drop_table("product_likes")
    op.drop_table("product_allergens")
    op.drop_table("product_nutrition")
    op.drop_table("product_attributes")
    op.drop_table("product_stickers")
    op.drop_table("product_labels")
    op.drop_table("product_sections")
    op.drop_table("product_images")
    op.drop_table("products")
    op.execute("DROP INDEX IF EXISTS ux_market_places_slug_active")
    op.drop_table("market_places")
