"""customers and sales

Revision ID: 32affdb9c7fb
Revises: 6d346a7a3aaa
Create Date: 2026-05-04 12:50:32.283803

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '32affdb9c7fb'
down_revision = '6d346a7a3aaa'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('customers',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=160), nullable=False),
    sa.Column('email', sa.String(length=190), nullable=True),
    sa.Column('phone', sa.String(length=40), nullable=True),
    sa.Column('document', sa.String(length=40), nullable=True),
    sa.Column('address', sa.String(length=300), nullable=True),
    sa.Column('city', sa.String(length=80), nullable=True),
    sa.Column('state', sa.String(length=80), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('customers', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_customers_document'), ['document'], unique=False)
        batch_op.create_index(batch_op.f('ix_customers_email'), ['email'], unique=False)
        batch_op.create_index(batch_op.f('ix_customers_name'), ['name'], unique=False)
        batch_op.create_index(batch_op.f('ix_customers_phone'), ['phone'], unique=False)
        batch_op.create_index(batch_op.f('ix_customers_user_id'), ['user_id'], unique=True)

    op.create_table('sales',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number', sa.String(length=20), nullable=False),
    sa.Column('channel', sa.Enum('WEB', 'COUNTER', name='salechannel', native_enum=False, length=10), nullable=False),
    sa.Column('status', sa.Enum('PENDING', 'CONFIRMED', 'CANCELLED', name='salestatus', native_enum=False, length=20), nullable=False),
    sa.Column('payment_status', sa.Enum('PENDING', 'PARTIAL', 'PAID', name='paymentstatus', native_enum=False, length=20), nullable=False),
    sa.Column('customer_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('subtotal', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('discount', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('total', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=8), 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.ForeignKeyConstraint(['customer_id'], ['customers.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('sales', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_sales_channel'), ['channel'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_customer_id'), ['customer_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_number'), ['number'], unique=True)
        batch_op.create_index(batch_op.f('ix_sales_payment_status'), ['payment_status'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_user_id'), ['user_id'], unique=False)

    op.create_table('sale_items',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('sale_id', sa.Integer(), nullable=False),
    sa.Column('product_id', sa.Integer(), nullable=False),
    sa.Column('product_item_id', sa.Integer(), nullable=True),
    sa.Column('unit_price', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('quantity', sa.Integer(), nullable=False),
    sa.Column('line_total', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
    sa.ForeignKeyConstraint(['product_item_id'], ['product_items.id'], ),
    sa.ForeignKeyConstraint(['sale_id'], ['sales.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('sale_items', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_sale_items_product_id'), ['product_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sale_items_product_item_id'), ['product_item_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sale_items_sale_id'), ['sale_id'], unique=False)

    # ### 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_index(batch_op.f('ix_sale_items_sale_id'))
        batch_op.drop_index(batch_op.f('ix_sale_items_product_item_id'))
        batch_op.drop_index(batch_op.f('ix_sale_items_product_id'))

    op.drop_table('sale_items')
    with op.batch_alter_table('sales', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_sales_user_id'))
        batch_op.drop_index(batch_op.f('ix_sales_status'))
        batch_op.drop_index(batch_op.f('ix_sales_payment_status'))
        batch_op.drop_index(batch_op.f('ix_sales_number'))
        batch_op.drop_index(batch_op.f('ix_sales_customer_id'))
        batch_op.drop_index(batch_op.f('ix_sales_created_at'))
        batch_op.drop_index(batch_op.f('ix_sales_channel'))

    op.drop_table('sales')
    with op.batch_alter_table('customers', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_customers_user_id'))
        batch_op.drop_index(batch_op.f('ix_customers_phone'))
        batch_op.drop_index(batch_op.f('ix_customers_name'))
        batch_op.drop_index(batch_op.f('ix_customers_email'))
        batch_op.drop_index(batch_op.f('ix_customers_document'))

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