"""currencies returns and payment exchange rate

Revision ID: 91ce00ca9acb
Revises: 145ce6700b8c
Create Date: 2026-05-08 13:42:52.348822

"""
from alembic import op
import sqlalchemy as sa


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


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('currencies',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('code', sa.String(length=8), nullable=False),
    sa.Column('symbol', sa.String(length=8), nullable=True),
    sa.Column('name', sa.String(length=80), nullable=False),
    sa.Column('is_primary', sa.Boolean(), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('currencies', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_currencies_code'), ['code'], unique=True)
        batch_op.create_index(batch_op.f('ix_currencies_is_active'), ['is_active'], unique=False)
        batch_op.create_index(batch_op.f('ix_currencies_is_primary'), ['is_primary'], unique=False)

    op.create_table('exchange_rates',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('currency_id', sa.Integer(), nullable=False),
    sa.Column('rate', sa.Numeric(precision=18, scale=6), nullable=False),
    sa.Column('valid_from', sa.DateTime(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['currency_id'], ['currencies.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('exchange_rates', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_exchange_rates_currency_id'), ['currency_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_exchange_rates_user_id'), ['user_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_exchange_rates_valid_from'), ['valid_from'], unique=False)

    op.create_table('returns',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number', sa.String(length=20), nullable=False),
    sa.Column('sale_id', sa.Integer(), nullable=True),
    sa.Column('customer_id', sa.Integer(), nullable=True),
    sa.Column('status', sa.Enum('PENDING', 'APPROVED', 'REJECTED', name='returnstatus', native_enum=False, length=20), nullable=False),
    sa.Column('reason', sa.String(length=120), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('processed_at', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ),
    sa.ForeignKeyConstraint(['sale_id'], ['sales.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('returns', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_returns_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_returns_customer_id'), ['customer_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_returns_number'), ['number'], unique=True)
        batch_op.create_index(batch_op.f('ix_returns_sale_id'), ['sale_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_returns_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_returns_user_id'), ['user_id'], unique=False)

    op.create_table('return_items',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('return_id', sa.Integer(), nullable=False),
    sa.Column('product_item_id', sa.Integer(), nullable=False),
    sa.Column('type', sa.Enum('REFUND_AVAILABLE', 'REFUND_DAMAGED', 'WARRANTY', 'LOSS', name='returntype', native_enum=False, length=30), nullable=False),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['product_item_id'], ['product_items.id'], ),
    sa.ForeignKeyConstraint(['return_id'], ['returns.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('return_items', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_return_items_product_item_id'), ['product_item_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_return_items_return_id'), ['return_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_return_items_type'), ['type'], unique=False)

    with op.batch_alter_table('payments', schema=None) as batch_op:
        batch_op.add_column(sa.Column('exchange_rate', sa.Numeric(precision=18, scale=6), nullable=True))

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('payments', schema=None) as batch_op:
        batch_op.drop_column('exchange_rate')

    with op.batch_alter_table('return_items', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_return_items_type'))
        batch_op.drop_index(batch_op.f('ix_return_items_return_id'))
        batch_op.drop_index(batch_op.f('ix_return_items_product_item_id'))

    op.drop_table('return_items')
    with op.batch_alter_table('returns', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_returns_user_id'))
        batch_op.drop_index(batch_op.f('ix_returns_status'))
        batch_op.drop_index(batch_op.f('ix_returns_sale_id'))
        batch_op.drop_index(batch_op.f('ix_returns_number'))
        batch_op.drop_index(batch_op.f('ix_returns_customer_id'))
        batch_op.drop_index(batch_op.f('ix_returns_created_at'))

    op.drop_table('returns')
    with op.batch_alter_table('exchange_rates', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_exchange_rates_valid_from'))
        batch_op.drop_index(batch_op.f('ix_exchange_rates_user_id'))
        batch_op.drop_index(batch_op.f('ix_exchange_rates_currency_id'))

    op.drop_table('exchange_rates')
    with op.batch_alter_table('currencies', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_currencies_is_primary'))
        batch_op.drop_index(batch_op.f('ix_currencies_is_active'))
        batch_op.drop_index(batch_op.f('ix_currencies_code'))

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