"""Add mtgonline_cards table for local card data mirror

Revision ID: 003
Revises: 002
Create Date: 2026-07-23 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Create mtgonline_cards table for local card data mirror."""
    op.create_table(
        'mtgonline_cards',
        sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column('source_id', sa.Integer(), nullable=True, index=True),  # References mtg_cards.id in mtgdata
        sa.Column('name', sa.String(255), nullable=False, index=True),
        sa.Column('mana_cost', sa.String(255), nullable=True),
        sa.Column('type_line', sa.String(255), nullable=True),
        sa.Column('oracle_text', sa.Text(), nullable=True),
        sa.Column('power', sa.String(50), nullable=True),
        sa.Column('toughness', sa.String(50), nullable=True),
        sa.Column('rarity', sa.String(50), nullable=True),
        sa.Column('layout', sa.String(50), nullable=True),
        sa.Column('artist', sa.String(255), nullable=True),
        sa.Column('flavor_text', sa.Text(), nullable=True),
        sa.Column('numbers', sa.String(100), nullable=True),
        sa.Column('identifiers', sa.Text(), nullable=True),  # JSON string
        sa.Column('images', sa.Text(), nullable=True),  # JSON string
        sa.Column('image', sa.Text(), nullable=True),  # Card image URL
        sa.Column('set_code', sa.String(10), nullable=True, index=True),
        sa.Column('set_name', sa.String(255), nullable=True),
        sa.Column('card_parts', sa.Text(), nullable=True),  # Comma-separated face names
        sa.Column('keywords', sa.Text(), nullable=True),  # Comma-separated keywords
        sa.Column('legalities', sa.Text(), nullable=True),  # JSON of format legality
        sa.Column('synced_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now()),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
    )
    op.create_index('idx_mtgonline_cards_name', 'mtgonline_cards', ['name'])
    op.create_index('idx_mtgonline_cards_set', 'mtgonline_cards', ['set_code'])


def downgrade() -> None:
    """Drop mtgonline_cards table."""
    op.drop_table('mtgonline_cards')
