# alembic/versions/p4d5e6f7a8b9_add_shipped_quantity_to_order_items.py
"""Add shipped_quantity to order_items for partial shipments.

Revision ID: p4d5e6f7a8b9
Revises: o3c4d5e6f7a8
Create Date: 2026-01-01 12:00:00.000000

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "p4d5e6f7a8b9"
down_revision: str | None = "o3c4d5e6f7a8"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Add shipped_quantity column to order_items
    op.add_column(
        "order_items",
        sa.Column("shipped_quantity", sa.Integer(), nullable=False, server_default="0")
    )

    # Set shipped_quantity = quantity for already fulfilled items
    # This handles existing data where inventory_fulfilled is True
    op.execute("""
        UPDATE order_items
        SET shipped_quantity = quantity
        WHERE inventory_fulfilled = true
    """)


def downgrade() -> None:
    op.drop_column("order_items", "shipped_quantity")
