"""product combos: kind + combo items + parent_sale_item_id

Revision ID: 6574b8389e05
Revises: ad6194739559
Create Date: 2026-05-09 12:16:09.256173

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6574b8389e05'
down_revision = 'ad6194739559'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('product_combo_items',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('combo_product_id', sa.Integer(), nullable=False),
    sa.Column('component_product_id', sa.Integer(), nullable=False),
    sa.Column('quantity', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.CheckConstraint('quantity > 0', name='ck_combo_qty_positive'),
    sa.ForeignKeyConstraint(['combo_product_id'], ['products.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['component_product_id'], ['products.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('combo_product_id', 'component_product_id', name='uq_combo_component')
    )
    with op.batch_alter_table('product_combo_items', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_product_combo_items_combo_product_id'), ['combo_product_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_product_combo_items_component_product_id'), ['component_product_id'], unique=False)

    with op.batch_alter_table('products', schema=None) as batch_op:
        batch_op.add_column(sa.Column('kind', sa.Enum('SIMPLE', 'COMBO', name='productkind', native_enum=False, length=20), nullable=False, server_default='SIMPLE'))
        batch_op.create_index(batch_op.f('ix_products_kind'), ['kind'], unique=False)

    with op.batch_alter_table('sale_items', schema=None) as batch_op:
        batch_op.add_column(sa.Column('parent_sale_item_id', sa.Integer(), nullable=True))
        batch_op.create_index(batch_op.f('ix_sale_items_parent_sale_item_id'), ['parent_sale_item_id'], unique=False)
        batch_op.create_foreign_key(None, 'sale_items', ['parent_sale_item_id'], ['id'])

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('sale_items', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_sale_items_parent_sale_item_id'))
        batch_op.drop_column('parent_sale_item_id')

    with op.batch_alter_table('products', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_products_kind'))
        batch_op.drop_column('kind')

    with op.batch_alter_table('product_combo_items', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_product_combo_items_component_product_id'))
        batch_op.drop_index(batch_op.f('ix_product_combo_items_combo_product_id'))

    op.drop_table('product_combo_items')
    # ### end Alembic commands ###
