"""Add user deck building tables

Revision ID: 002
Revises: 001
Create Date: 2026-01-02 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Create user deck building tables."""
    
    # 1. User Decks Table
    op.create_table(
        'user_decks',
        sa.Column('id', sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True, autoincrement=True),
        sa.Column('user_id', sa.Integer(), sa.ForeignKey('mtgonline_users.id', ondelete='CASCADE'), nullable=False, index=True),
        sa.Column('name', sa.String(255), nullable=False, index=True),
        sa.Column('status', sa.String(20), nullable=False, default='DRAFT', index=True),
        sa.Column('folder_id', sa.Integer(), sa.ForeignKey('mtgonline_decklist_folders.id'), nullable=True),
        sa.Column('format', sa.String(50), nullable=True, default='standard'),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('is_precedent', sa.Boolean(), default=False, index=True),
        sa.Column('precedent_name', sa.String(255), 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()),
    )
    
    # 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(), 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(), 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(), nullable=False, index=True),
        sa.Column('source_card_id', sa.Integer(), 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 user deck building 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('user_decks')
