"""create mtgjson tables

Revision ID: 2025_04_13_create_mtgjson_tables
Revises: 2025_04_09_create_tcgplayer_categories_table
Create Date: 2025-04-13 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2025_04_13_create_mtgjson_tables'
down_revision = '2025_04_09_create_tcgplayer_categories_table'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Create mtgjson_cards table
    op.create_table(
        'mtgjson_cards',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('card_id', sa.String(), nullable=False),
        sa.Column('name', sa.String(), nullable=True),
        sa.Column('set_code', sa.String(), nullable=True),
        sa.Column('uuid', sa.String(), nullable=True),
        # Identifiers
        sa.Column('abu_id', sa.String(), nullable=True),
        sa.Column('card_kingdom_etched_id', sa.String(), nullable=True),
        sa.Column('card_kingdom_foil_id', sa.String(), nullable=True),
        sa.Column('card_kingdom_id', sa.String(), nullable=True),
        sa.Column('cardsphere_id', sa.String(), nullable=True),
        sa.Column('cardsphere_foil_id', sa.String(), nullable=True),
        sa.Column('cardtrader_id', sa.String(), nullable=True),
        sa.Column('csi_id', sa.String(), nullable=True),
        sa.Column('mcm_id', sa.String(), nullable=True),
        sa.Column('mcm_meta_id', sa.String(), nullable=True),
        sa.Column('miniaturemarket_id', sa.String(), nullable=True),
        sa.Column('mtg_arena_id', sa.String(), nullable=True),
        sa.Column('mtgjson_foil_version_id', sa.String(), nullable=True),
        sa.Column('mtgjson_non_foil_version_id', sa.String(), nullable=True),
        sa.Column('mtgjson_v4_id', sa.String(), nullable=True),
        sa.Column('mtgo_foil_id', sa.String(), nullable=True),
        sa.Column('mtgo_id', sa.String(), nullable=True),
        sa.Column('multiverse_id', sa.String(), nullable=True),
        sa.Column('scg_id', sa.String(), nullable=True),
        sa.Column('scryfall_id', sa.String(), nullable=True),
        sa.Column('scryfall_card_back_id', sa.String(), nullable=True),
        sa.Column('scryfall_oracle_id', sa.String(), nullable=True),
        sa.Column('scryfall_illustration_id', sa.String(), nullable=True),
        sa.Column('tcgplayer_product_id', sa.String(), nullable=True),
        sa.Column('tcgplayer_etched_product_id', sa.String(), nullable=True),
        sa.Column('tnt_id', sa.String(), nullable=True),
        sa.Column('data', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_mtgjson_cards_card_id'), 'mtgjson_cards', ['card_id'], unique=True)
    op.create_index(op.f('ix_mtgjson_cards_id'), 'mtgjson_cards', ['id'], unique=False)

    # Create mtgjson_skus table
    op.create_table(
        'mtgjson_skus',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('sku_id', sa.String(), nullable=False),
        sa.Column('product_id', sa.String(), nullable=True),
        sa.Column('condition', sa.String(), nullable=True),
        sa.Column('finish', sa.String(), nullable=True),
        sa.Column('language', sa.String(), nullable=True),
        sa.Column('printing', sa.String(), nullable=True),
        sa.Column('card_id', sa.String(), nullable=True),
        sa.Column('data', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_mtgjson_skus_card_id'), 'mtgjson_skus', ['card_id'], unique=False)
    op.create_index(op.f('ix_mtgjson_skus_id'), 'mtgjson_skus', ['id'], unique=False)
    op.create_index(op.f('ix_mtgjson_skus_product_id'), 'mtgjson_skus', ['product_id'], unique=False)
    op.create_index(op.f('ix_mtgjson_skus_sku_id'), 'mtgjson_skus', ['sku_id'], unique=False)


def downgrade() -> None:
    # Drop mtgjson_skus table first due to foreign key constraint
    op.drop_index(op.f('ix_mtgjson_skus_sku_id'), table_name='mtgjson_skus')
    op.drop_index(op.f('ix_mtgjson_skus_product_id'), table_name='mtgjson_skus')
    op.drop_index(op.f('ix_mtgjson_skus_id'), table_name='mtgjson_skus')
    op.drop_index(op.f('ix_mtgjson_skus_card_id'), table_name='mtgjson_skus')
    op.drop_table('mtgjson_skus')

    # Drop mtgjson_cards table
    op.drop_index(op.f('ix_mtgjson_cards_id'), table_name='mtgjson_cards')
    op.drop_index(op.f('ix_mtgjson_cards_card_id'), table_name='mtgjson_cards')
    op.drop_table('mtgjson_cards') 