"""create_tcgplayer_order_tables

Revision ID: 6f2b3f870fdf
Revises: fix_foreign_key_issue
Create Date: 2025-04-16 20:19:01.698636

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSON


# revision identifiers, used by Alembic.
revision: str = '6f2b3f870fdf'
down_revision: Union[str, None] = 'fix_foreign_key_issue'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create tcgplayer_orders table
    op.create_table(
        'tcgplayer_orders',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('order_number', sa.String(), nullable=True),
        sa.Column('order_created_at', sa.DateTime(), nullable=True),
        sa.Column('status', sa.String(), nullable=True),
        sa.Column('channel', sa.String(), nullable=True),
        sa.Column('fulfillment', sa.String(), nullable=True),
        sa.Column('seller_name', sa.String(), nullable=True),
        sa.Column('buyer_name', sa.String(), nullable=True),
        sa.Column('payment_type', sa.String(), nullable=True),
        sa.Column('pickup_status', sa.String(), nullable=True),
        sa.Column('shipping_type', sa.String(), nullable=True),
        sa.Column('estimated_delivery_date', sa.DateTime(), nullable=True),
        sa.Column('recipient_name', sa.String(), nullable=True),
        sa.Column('address_line_1', sa.String(), nullable=True),
        sa.Column('address_line_2', sa.String(), nullable=True),
        sa.Column('city', sa.String(), nullable=True),
        sa.Column('state', sa.String(), nullable=True),
        sa.Column('zip_code', sa.String(), nullable=True),
        sa.Column('country', sa.String(), nullable=True),
        sa.Column('tracking_numbers', JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tcgplayer_orders_id'), 'tcgplayer_orders', ['id'], unique=False)
    op.create_index(op.f('ix_tcgplayer_orders_order_number'), 'tcgplayer_orders', ['order_number'], unique=False)

    # Create tcgplayer_order_transactions table
    op.create_table(
        'tcgplayer_order_transactions',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('order_number', sa.String(), nullable=True),
        sa.Column('product_amount', sa.Float(), nullable=True),
        sa.Column('shipping_amount', sa.Float(), nullable=True),
        sa.Column('gross_amount', sa.Float(), nullable=True),
        sa.Column('fee_amount', sa.Float(), nullable=True),
        sa.Column('net_amount', sa.Float(), nullable=True),
        sa.Column('direct_fee_amount', sa.Float(), nullable=True),
        sa.Column('taxes', JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tcgplayer_order_transactions_id'), 'tcgplayer_order_transactions', ['id'], unique=False)
    op.create_index(op.f('ix_tcgplayer_order_transactions_order_number'), 'tcgplayer_order_transactions', ['order_number'], unique=False)

    # Create tcgplayer_order_products table
    op.create_table(
        'tcgplayer_order_products',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('order_number', sa.String(), nullable=True),
        sa.Column('product_name', sa.String(), nullable=True),
        sa.Column('unit_price', sa.Float(), nullable=True),
        sa.Column('extended_price', sa.Float(), nullable=True),
        sa.Column('quantity', sa.Integer(), nullable=True),
        sa.Column('url', sa.String(), nullable=True),
        sa.Column('product_id', sa.String(), nullable=True),
        sa.Column('sku_id', sa.String(), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tcgplayer_order_products_id'), 'tcgplayer_order_products', ['id'], unique=False)
    op.create_index(op.f('ix_tcgplayer_order_products_order_number'), 'tcgplayer_order_products', ['order_number'], unique=False)

    # Create tcgplayer_order_refunds table
    op.create_table(
        'tcgplayer_order_refunds',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('order_number', sa.String(), nullable=True),
        sa.Column('refund_created_at', sa.DateTime(), nullable=True),
        sa.Column('type', sa.String(), nullable=True),
        sa.Column('amount', sa.Float(), nullable=True),
        sa.Column('description', sa.String(), nullable=True),
        sa.Column('origin', sa.String(), nullable=True),
        sa.Column('shipping_amount', sa.Float(), nullable=True),
        sa.Column('products', JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_tcgplayer_order_refunds_id'), 'tcgplayer_order_refunds', ['id'], unique=False)
    op.create_index(op.f('ix_tcgplayer_order_refunds_order_number'), 'tcgplayer_order_refunds', ['order_number'], unique=False)


def downgrade() -> None:
    op.drop_index(op.f('ix_tcgplayer_order_refunds_order_number'), table_name='tcgplayer_order_refunds')
    op.drop_index(op.f('ix_tcgplayer_order_refunds_id'), table_name='tcgplayer_order_refunds')
    op.drop_table('tcgplayer_order_refunds')

    op.drop_index(op.f('ix_tcgplayer_order_products_order_number'), table_name='tcgplayer_order_products')
    op.drop_index(op.f('ix_tcgplayer_order_products_id'), table_name='tcgplayer_order_products')
    op.drop_table('tcgplayer_order_products')

    op.drop_index(op.f('ix_tcgplayer_order_transactions_order_number'), table_name='tcgplayer_order_transactions')
    op.drop_index(op.f('ix_tcgplayer_order_transactions_id'), table_name='tcgplayer_order_transactions')
    op.drop_table('tcgplayer_order_transactions')

    op.drop_index(op.f('ix_tcgplayer_orders_order_number'), table_name='tcgplayer_orders')
    op.drop_index(op.f('ix_tcgplayer_orders_id'), table_name='tcgplayer_orders')
    op.drop_table('tcgplayer_orders')
