"""orders initial - orders, order items, exceptions, invoices, fulfillment queue

Revision ID: orders_001
Revises: customers_001
Create Date: 2026-02-07
"""
import sqlalchemy as sa

from alembic import op

revision = "orders_001"
down_revision = "customers_001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # --- orders ---
    op.create_table(
        "orders",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("customer_id", sa.Integer(), sa.ForeignKey("customers.id"), nullable=False, index=True),
        sa.Column("order_number", sa.String(100), unique=True, nullable=False, index=True),
        sa.Column("channel", sa.String(50), nullable=False, server_default="direct", index=True),
        sa.Column("external_order_id", sa.String(100), nullable=True, index=True),
        sa.Column("external_shipment_id", sa.String(100), nullable=True, index=True),
        sa.Column("external_order_number", sa.String(100), nullable=True),
        sa.Column("external_data", sa.JSON(), nullable=True),
        sa.Column("status", sa.String(50), nullable=False, server_default="pending", index=True),
        sa.Column("subtotal_cents", sa.Integer(), nullable=True),
        sa.Column("tax_amount_cents", sa.Integer(), nullable=True),
        sa.Column("shipping_amount_cents", sa.Integer(), nullable=True),
        sa.Column("discount_amount_cents", sa.Integer(), nullable=True),
        sa.Column("total_amount_cents", sa.Integer(), nullable=False),
        sa.Column("currency", sa.String(10), nullable=True, server_default="EUR"),
        sa.Column("vat_regime", sa.String(20), nullable=True),
        sa.Column("vat_rate", sa.Numeric(5, 2), nullable=True),
        sa.Column("vat_rate_label", sa.String(100), nullable=True),
        sa.Column("vat_destination_country", sa.String(2), nullable=True),
        sa.Column("customer_first_name", sa.String(100), nullable=False),
        sa.Column("customer_last_name", sa.String(100), nullable=False),
        sa.Column("customer_email", sa.String(255), nullable=False),
        sa.Column("customer_phone", sa.String(50), nullable=True),
        sa.Column("customer_locale", sa.String(10), nullable=True),
        sa.Column("ship_first_name", sa.String(100), nullable=False),
        sa.Column("ship_last_name", sa.String(100), nullable=False),
        sa.Column("ship_company", sa.String(200), nullable=True),
        sa.Column("ship_address_line_1", sa.String(255), nullable=False),
        sa.Column("ship_address_line_2", sa.String(255), nullable=True),
        sa.Column("ship_city", sa.String(100), nullable=False),
        sa.Column("ship_postal_code", sa.String(20), nullable=False),
        sa.Column("ship_country_iso", sa.String(5), nullable=False),
        sa.Column("bill_first_name", sa.String(100), nullable=False),
        sa.Column("bill_last_name", sa.String(100), nullable=False),
        sa.Column("bill_company", sa.String(200), nullable=True),
        sa.Column("bill_address_line_1", sa.String(255), nullable=False),
        sa.Column("bill_address_line_2", sa.String(255), nullable=True),
        sa.Column("bill_city", sa.String(100), nullable=False),
        sa.Column("bill_postal_code", sa.String(20), nullable=False),
        sa.Column("bill_country_iso", sa.String(5), nullable=False),
        sa.Column("shipping_method", sa.String(100), nullable=True),
        sa.Column("tracking_number", sa.String(100), nullable=True),
        sa.Column("tracking_provider", sa.String(100), nullable=True),
        sa.Column("tracking_url", sa.String(500), nullable=True),
        sa.Column("shipment_number", sa.String(100), nullable=True),
        sa.Column("shipping_carrier", sa.String(50), nullable=True),
        sa.Column("customer_notes", sa.Text(), nullable=True),
        sa.Column("internal_notes", sa.Text(), nullable=True),
        sa.Column("order_date", sa.DateTime(timezone=True), nullable=False),
        sa.Column("confirmed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("shipped_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("delivered_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("cancelled_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("idx_order_store_status", "orders", ["store_id", "status"])
    op.create_index("idx_order_store_channel", "orders", ["store_id", "channel"])
    op.create_index("idx_order_store_date", "orders", ["store_id", "order_date"])

    # --- order_items ---
    op.create_table(
        "order_items",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("order_id", sa.Integer(), sa.ForeignKey("orders.id"), nullable=False, index=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id"), nullable=False),
        sa.Column("product_name", sa.String(255), nullable=False),
        sa.Column("product_sku", sa.String(100), nullable=True),
        sa.Column("gtin", sa.String(50), nullable=True),
        sa.Column("gtin_type", sa.String(20), nullable=True),
        sa.Column("quantity", sa.Integer(), nullable=False),
        sa.Column("unit_price_cents", sa.Integer(), nullable=False),
        sa.Column("total_price_cents", sa.Integer(), nullable=False),
        sa.Column("external_item_id", sa.String(100), nullable=True),
        sa.Column("external_variant_id", sa.String(100), nullable=True),
        sa.Column("item_state", sa.String(50), nullable=True),
        sa.Column("inventory_reserved", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("inventory_fulfilled", sa.Boolean(), nullable=True, server_default="false"),
        sa.Column("shipped_quantity", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("needs_product_match", sa.Boolean(), nullable=True, server_default="false", index=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )

    # --- order_item_exceptions ---
    op.create_table(
        "order_item_exceptions",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("order_item_id", sa.Integer(), sa.ForeignKey("order_items.id", ondelete="CASCADE"), unique=True, nullable=False),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("original_gtin", sa.String(50), nullable=True, index=True),
        sa.Column("original_product_name", sa.String(500), nullable=True),
        sa.Column("original_sku", sa.String(100), nullable=True),
        sa.Column("exception_type", sa.String(50), nullable=False, server_default="product_not_found"),
        sa.Column("status", sa.String(50), nullable=False, server_default="pending", index=True),
        sa.Column("resolved_product_id", sa.Integer(), sa.ForeignKey("products.id"), nullable=True),
        sa.Column("resolved_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("resolved_by", sa.Integer(), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("resolution_notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("idx_exception_store_status", "order_item_exceptions", ["store_id", "status"])
    op.create_index("idx_exception_gtin", "order_item_exceptions", ["store_id", "original_gtin"])

    # --- store_invoice_settings ---
    op.create_table(
        "store_invoice_settings",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), unique=True, nullable=False, index=True),
        sa.Column("merchant_name", sa.String(255), nullable=False),
        sa.Column("merchant_address", sa.String(255), nullable=True),
        sa.Column("merchant_city", sa.String(100), nullable=True),
        sa.Column("merchant_postal_code", sa.String(20), nullable=True),
        sa.Column("merchant_country", sa.String(2), nullable=False, server_default="LU"),
        sa.Column("vat_number", sa.String(50), nullable=True),
        sa.Column("is_vat_registered", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("is_oss_registered", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("oss_registration_country", sa.String(2), nullable=True),
        sa.Column("invoice_prefix", sa.String(20), nullable=False, server_default="INV"),
        sa.Column("invoice_next_number", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("invoice_number_padding", sa.Integer(), nullable=False, server_default="5"),
        sa.Column("payment_terms", sa.Text(), nullable=True),
        sa.Column("bank_name", sa.String(255), nullable=True),
        sa.Column("bank_iban", sa.String(50), nullable=True),
        sa.Column("bank_bic", sa.String(20), nullable=True),
        sa.Column("footer_text", sa.Text(), nullable=True),
        sa.Column("default_vat_rate", sa.Numeric(5, 2), nullable=False, server_default="17.00"),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )

    # --- invoices ---
    op.create_table(
        "invoices",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("order_id", sa.Integer(), sa.ForeignKey("orders.id"), nullable=True, index=True),
        sa.Column("invoice_number", sa.String(50), nullable=False),
        sa.Column("invoice_date", sa.DateTime(timezone=True), nullable=False),
        sa.Column("status", sa.String(20), nullable=False, server_default="draft"),
        sa.Column("seller_details", sa.JSON(), nullable=False),
        sa.Column("buyer_details", sa.JSON(), nullable=False),
        sa.Column("line_items", sa.JSON(), nullable=False),
        sa.Column("vat_regime", sa.String(20), nullable=False, server_default="domestic"),
        sa.Column("destination_country", sa.String(2), nullable=True),
        sa.Column("vat_rate", sa.Numeric(5, 2), nullable=False),
        sa.Column("vat_rate_label", sa.String(50), nullable=True),
        sa.Column("currency", sa.String(3), nullable=False, server_default="EUR"),
        sa.Column("subtotal_cents", sa.Integer(), nullable=False),
        sa.Column("vat_amount_cents", sa.Integer(), nullable=False),
        sa.Column("total_cents", sa.Integer(), nullable=False),
        sa.Column("payment_terms", sa.Text(), nullable=True),
        sa.Column("bank_details", sa.JSON(), nullable=True),
        sa.Column("footer_text", sa.Text(), nullable=True),
        sa.Column("pdf_generated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("pdf_path", sa.String(500), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("idx_invoice_store_number", "invoices", ["store_id", "invoice_number"], unique=True)
    op.create_index("idx_invoice_store_date", "invoices", ["store_id", "invoice_date"])
    op.create_index("idx_invoice_status", "invoices", ["store_id", "status"])

    # --- letzshop_fulfillment_queue (depends on orders + stores) ---
    op.create_table(
        "letzshop_fulfillment_queue",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("order_id", sa.Integer(), sa.ForeignKey("orders.id"), nullable=False, index=True),
        sa.Column("operation", sa.String(50), nullable=False),
        sa.Column("payload", sa.JSON(), nullable=False),
        sa.Column("status", sa.String(50), nullable=True, server_default="pending"),
        sa.Column("attempts", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("max_attempts", sa.Integer(), nullable=True, server_default="3"),
        sa.Column("last_attempt_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("next_retry_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("response_data", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("idx_fulfillment_queue_status", "letzshop_fulfillment_queue", ["status", "store_id"])
    op.create_index("idx_fulfillment_queue_retry", "letzshop_fulfillment_queue", ["status", "next_retry_at"])
    op.create_index("idx_fulfillment_queue_order", "letzshop_fulfillment_queue", ["order_id"])


def downgrade() -> None:
    op.drop_table("letzshop_fulfillment_queue")
    op.drop_table("invoices")
    op.drop_table("store_invoice_settings")
    op.drop_table("order_item_exceptions")
    op.drop_table("order_items")
    op.drop_table("orders")
