"""Add mtgonline_cards table and deck building junction tables

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 and deck building junction tables."""
    
    # 1. 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'])
    
    # 2. User Deck Cards Junction Table
    op.create_table(
        'user_deck_cards',
        sa.Column('id', sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True, autoincrement=True),
        sa.Column('deck_id', sa.BigInteger(), sa.ForeignKey('user_decks.id', ondelete='CASCADE'), nullable=False, index=True),
        sa.Column('card_id', sa.Integer(), sa.ForeignKey('mtgonline_cards.id'), nullable=False, index=True),
        sa.Column('quantity', sa.Integer(), nullable=False, default=1),
        sa.Column('zone', sa.String(20), nullable=False, default='main'),
        sa.Column('position', sa.Integer(), nullable=True),
    )
    op.create_unique_constraint(
        'uq_deck_card_unique',
        'user_deck_cards',
        ['deck_id', 'card_id', 'zone']
    )
    op.create_index('idx_deck_cards_deck', 'user_deck_cards', ['deck_id'])
    op.create_index('idx_deck_cards_card', 'user_deck_cards', ['card_id'])
    
    # 3. Deck Precedents Table
    op.create_table(
        'deck_precedents',
        sa.Column('id', sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True, autoincrement=True),
        sa.Column('name', sa.String(255), nullable=False, index=True),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('format', sa.String(50), nullable=True, default='standard'),
        sa.Column('is_public', sa.Boolean(), default=True, index=True),
        sa.Column('created_by', sa.Integer(), sa.ForeignKey('mtgonline_users.id'), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now()),
    )
    
    # 4. Deck Precedent Cards Junction Table
    op.create_table(
        'deck_precedent_cards',
        sa.Column('id', sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True, autoincrement=True),
        sa.Column('precedent_id', sa.BigInteger(), sa.ForeignKey('deck_precedents.id', ondelete='CASCADE'), nullable=False, index=True),
        sa.Column('card_id', sa.Integer(), sa.ForeignKey('mtgonline_cards.id'), nullable=False, index=True),
        sa.Column('quantity', sa.Integer(), nullable=False, default=1),
        sa.Column('zone', sa.String(20), nullable=False, default='main'),
    )
    op.create_unique_constraint(
        'uq_precedent_card_unique',
        'deck_precedent_cards',
        ['precedent_id', 'card_id', 'zone']
    )
    
    # 5. Card Suggestions Table
    op.create_table(
        'card_suggestions',
        sa.Column('id', sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True, autoincrement=True),
        sa.Column('deck_id', sa.BigInteger(), sa.ForeignKey('user_decks.id', ondelete='CASCADE'), nullable=False, index=True),
        sa.Column('card_id', sa.Integer(), sa.ForeignKey('mtgonline_cards.id'), nullable=False, index=True),
        sa.Column('source_card_id', sa.Integer(), sa.ForeignKey('mtgonline_cards.id'), nullable=True),
        sa.Column('suggestion_type', sa.String(50), nullable=False, default='SIMILAR'),
        sa.Column('confidence', sa.Float(), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
    )
    op.create_unique_constraint(
        'uq_suggestion_unique',
        'card_suggestions',
        ['deck_id', 'card_id', 'source_card_id']
    )


def downgrade() -> None:
    """Drop mtgonline_cards and deck building junction tables."""
    op.drop_table('card_suggestions')
    op.drop_table('deck_precedent_cards')
    op.drop_table('deck_precedents')
    op.drop_table('user_deck_cards')
    op.drop_table('mtgonline_cards')
