"""remove product_id unique constraint

Revision ID: remove_product_id_unique_constraint
Revises: 4dbeb89dd33a
Create Date: 2025-04-09 23:10:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'remove_product_id_unique_constraint'
down_revision: str = '4dbeb89dd33a'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create a new table without the unique constraint
    op.create_table('tcgplayer_products_new',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('product_id', sa.Integer(), nullable=True),
        sa.Column('name', sa.String(), nullable=False),
        sa.Column('clean_name', sa.String(), nullable=True),
        sa.Column('image_url', sa.String(), nullable=True),
        sa.Column('category_id', sa.Integer(), nullable=True),
        sa.Column('group_id', sa.Integer(), nullable=True),
        sa.Column('url', sa.String(), nullable=True),
        sa.Column('modified_on', sa.DateTime(), nullable=True),
        sa.Column('image_count', sa.Integer(), nullable=True),
        sa.Column('ext_rarity', sa.String(), nullable=True),
        sa.Column('ext_number', sa.String(), nullable=True),
        sa.Column('low_price', sa.Float(), nullable=True),
        sa.Column('mid_price', sa.Float(), nullable=True),
        sa.Column('high_price', sa.Float(), nullable=True),
        sa.Column('market_price', sa.Float(), nullable=True),
        sa.Column('direct_low_price', sa.Float(), nullable=True),
        sa.Column('sub_type_name', sa.String(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.ForeignKeyConstraint(['group_id'], ['tcgplayer_groups.group_id'])
    )
    
    # Copy data from old table to new table
    op.execute('INSERT INTO tcgplayer_products_new SELECT * FROM tcgplayer_products')
    
    # Drop old table
    op.drop_table('tcgplayer_products')
    
    # Rename new table to old table name
    op.rename_table('tcgplayer_products_new', 'tcgplayer_products')
    
    # Create indexes
    op.create_index('ix_tcgplayer_products_id', 'tcgplayer_products', ['id'], unique=False)
    op.create_index('ix_tcgplayer_products_product_id', 'tcgplayer_products', ['product_id'], unique=False)


def downgrade() -> None:
    # Create a new table with the unique constraint
    op.create_table('tcgplayer_products_new',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('product_id', sa.Integer(), nullable=True),
        sa.Column('name', sa.String(), nullable=False),
        sa.Column('clean_name', sa.String(), nullable=True),
        sa.Column('image_url', sa.String(), nullable=True),
        sa.Column('category_id', sa.Integer(), nullable=True),
        sa.Column('group_id', sa.Integer(), nullable=True),
        sa.Column('url', sa.String(), nullable=True),
        sa.Column('modified_on', sa.DateTime(), nullable=True),
        sa.Column('image_count', sa.Integer(), nullable=True),
        sa.Column('ext_rarity', sa.String(), nullable=True),
        sa.Column('ext_number', sa.String(), nullable=True),
        sa.Column('low_price', sa.Float(), nullable=True),
        sa.Column('mid_price', sa.Float(), nullable=True),
        sa.Column('high_price', sa.Float(), nullable=True),
        sa.Column('market_price', sa.Float(), nullable=True),
        sa.Column('direct_low_price', sa.Float(), nullable=True),
        sa.Column('sub_type_name', sa.String(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('product_id'),
        sa.ForeignKeyConstraint(['group_id'], ['tcgplayer_groups.group_id'])
    )
    
    # Copy data from old table to new table
    op.execute('INSERT INTO tcgplayer_products_new SELECT * FROM tcgplayer_products')
    
    # Drop old table
    op.drop_table('tcgplayer_products')
    
    # Rename new table to old table name
    op.rename_table('tcgplayer_products_new', 'tcgplayer_products')
    
    # Create indexes
    op.create_index('ix_tcgplayer_products_id', 'tcgplayer_products', ['id'], unique=False)
    op.create_index('ix_tcgplayer_products_product_id', 'tcgplayer_products', ['product_id'], unique=True) 