"""stores transfers and store_id columns

Revision ID: 879c29caee90
Revises: 91ce00ca9acb
Create Date: 2026-05-08 15:56:38.405429

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '879c29caee90'
down_revision = '91ce00ca9acb'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('stores',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('code', sa.String(length=20), nullable=False),
    sa.Column('name', sa.String(length=120), nullable=False),
    sa.Column('address', sa.String(length=300), nullable=True),
    sa.Column('phone', sa.String(length=40), nullable=True),
    sa.Column('email', sa.String(length=190), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_main', sa.Boolean(), nullable=False),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('stores', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_stores_code'), ['code'], unique=True)
        batch_op.create_index(batch_op.f('ix_stores_is_active'), ['is_active'], unique=False)
        batch_op.create_index(batch_op.f('ix_stores_is_main'), ['is_main'], unique=False)
        batch_op.create_index(batch_op.f('ix_stores_name'), ['name'], unique=False)

    op.create_table('stock_transfers',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number', sa.String(length=20), nullable=False),
    sa.Column('from_store_id', sa.Integer(), nullable=False),
    sa.Column('to_store_id', sa.Integer(), nullable=False),
    sa.Column('status', sa.Enum('DRAFT', 'IN_TRANSIT', 'RECEIVED', 'CANCELLED', name='transferstatus', native_enum=False, length=20), nullable=False),
    sa.Column('sent_at', sa.DateTime(), nullable=True),
    sa.Column('received_at', sa.DateTime(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['from_store_id'], ['stores.id'], ),
    sa.ForeignKeyConstraint(['to_store_id'], ['stores.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('stock_transfers', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_stock_transfers_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_stock_transfers_from_store_id'), ['from_store_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_stock_transfers_number'), ['number'], unique=True)
        batch_op.create_index(batch_op.f('ix_stock_transfers_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_stock_transfers_to_store_id'), ['to_store_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_stock_transfers_user_id'), ['user_id'], unique=False)

    op.create_table('user_store_assignments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('store_id', sa.Integer(), nullable=False),
    sa.Column('can_sell', sa.Boolean(), nullable=False),
    sa.Column('can_manage_inventory', sa.Boolean(), nullable=False),
    sa.Column('is_default', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['store_id'], ['stores.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'store_id', name='uq_user_store')
    )
    with op.batch_alter_table('user_store_assignments', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_user_store_assignments_store_id'), ['store_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_user_store_assignments_user_id'), ['user_id'], unique=False)

    op.create_table('stock_transfer_items',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('transfer_id', sa.Integer(), nullable=False),
    sa.Column('product_item_id', sa.Integer(), nullable=False),
    sa.Column('received', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['product_item_id'], ['product_items.id'], ),
    sa.ForeignKeyConstraint(['transfer_id'], ['stock_transfers.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('stock_transfer_items', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_stock_transfer_items_product_item_id'), ['product_item_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_stock_transfer_items_transfer_id'), ['transfer_id'], unique=False)

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

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

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

    # ### end Alembic commands ###


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

    with op.batch_alter_table('purchases', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_purchases_store_id'))
        batch_op.drop_column('store_id')

    with op.batch_alter_table('product_items', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_product_items_store_id'))
        batch_op.drop_column('store_id')

    with op.batch_alter_table('stock_transfer_items', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_stock_transfer_items_transfer_id'))
        batch_op.drop_index(batch_op.f('ix_stock_transfer_items_product_item_id'))

    op.drop_table('stock_transfer_items')
    with op.batch_alter_table('user_store_assignments', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_user_store_assignments_user_id'))
        batch_op.drop_index(batch_op.f('ix_user_store_assignments_store_id'))

    op.drop_table('user_store_assignments')
    with op.batch_alter_table('stock_transfers', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_stock_transfers_user_id'))
        batch_op.drop_index(batch_op.f('ix_stock_transfers_to_store_id'))
        batch_op.drop_index(batch_op.f('ix_stock_transfers_status'))
        batch_op.drop_index(batch_op.f('ix_stock_transfers_number'))
        batch_op.drop_index(batch_op.f('ix_stock_transfers_from_store_id'))
        batch_op.drop_index(batch_op.f('ix_stock_transfers_created_at'))

    op.drop_table('stock_transfers')
    with op.batch_alter_table('stores', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_stores_name'))
        batch_op.drop_index(batch_op.f('ix_stores_is_main'))
        batch_op.drop_index(batch_op.f('ix_stores_is_active'))
        batch_op.drop_index(batch_op.f('ix_stores_code'))

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