"""inventory initial - inventory, inventory transactions

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

from alembic import op

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


def upgrade() -> None:
    # --- inventory ---
    op.create_table(
        "inventory",
        sa.Column("id", sa.Integer(), primary_key=True, index=True),
        sa.Column("product_id", sa.Integer(), sa.ForeignKey("products.id"), nullable=False, index=True),
        sa.Column("store_id", sa.Integer(), sa.ForeignKey("stores.id"), nullable=False, index=True),
        sa.Column("warehouse", sa.String(), nullable=False, server_default="strassen", index=True),
        sa.Column("bin_location", sa.String(), nullable=False, index=True),
        sa.Column("quantity", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("reserved_quantity", sa.Integer(), nullable=True, server_default="0"),
        sa.Column("gtin", sa.String(), nullable=True, 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),
        sa.UniqueConstraint("product_id", "warehouse", "bin_location", name="uq_inventory_product_warehouse_bin"),
    )
    op.create_index("idx_inventory_store_product", "inventory", ["store_id", "product_id"])
    op.create_index("idx_inventory_warehouse_bin", "inventory", ["warehouse", "bin_location"])

    # --- inventory_transactions ---
    # Create the enum type for transaction_type
    transaction_type_enum = sa.Enum(
        "reserve", "fulfill", "release", "adjust", "set", "import", "return",
        name="transactiontype",
    )
    op.create_table(
        "inventory_transactions",
        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("product_id", sa.Integer(), sa.ForeignKey("products.id"), nullable=False, index=True),
        sa.Column("inventory_id", sa.Integer(), sa.ForeignKey("inventory.id"), nullable=True, index=True),
        sa.Column("transaction_type", transaction_type_enum, nullable=False, index=True),
        sa.Column("quantity_change", sa.Integer(), nullable=False),
        sa.Column("quantity_after", sa.Integer(), nullable=False),
        sa.Column("reserved_after", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("location", sa.String(), nullable=True),
        sa.Column("warehouse", sa.String(), nullable=True),
        sa.Column("order_id", sa.Integer(), sa.ForeignKey("orders.id"), nullable=True, index=True),
        sa.Column("order_number", sa.String(), nullable=True),
        sa.Column("reason", sa.Text(), nullable=True),
        sa.Column("created_by", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False, index=True),
    )
    op.create_index("idx_inv_tx_store_product", "inventory_transactions", ["store_id", "product_id"])
    op.create_index("idx_inv_tx_store_created", "inventory_transactions", ["store_id", "created_at"])
    op.create_index("idx_inv_tx_order", "inventory_transactions", ["order_id"])
    op.create_index("idx_inv_tx_type_created", "inventory_transactions", ["transaction_type", "created_at"])


def downgrade() -> None:
    op.drop_table("inventory_transactions")
    op.drop_table("inventory")
    sa.Enum(name="transactiontype").drop(op.get_bind(), checkfirst=True)
