"""Create matches and predictions tables

Revision ID: 0006
Revises: 0004
Create Date: 2026-01-17 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0006'
down_revision = '0004'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Create matches and predictions tables."""
    # Create matches table
    op.create_table(
        'matches',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('home_team', sa.String(length=255), nullable=False),
        sa.Column('away_team', sa.String(length=255), nullable=False),
        sa.Column('date', sa.DateTime(), nullable=False),
        sa.Column('league', sa.String(length=255), nullable=False),
        sa.Column('status', sa.String(length=50), nullable=False),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_matches_id'), 'matches', ['id'], unique=False)
    op.create_index(op.f('ix_matches_home_team'), 'matches', ['home_team'], unique=False)
    op.create_index(op.f('ix_matches_away_team'), 'matches', ['away_team'], unique=False)
    op.create_index(op.f('ix_matches_date'), 'matches', ['date'], unique=False)
    op.create_index(op.f('ix_matches_league'), 'matches', ['league'], unique=False)
    op.create_index(op.f('ix_matches_status'), 'matches', ['status'], unique=False)
    op.create_index(op.f('ix_matches_date_league'), 'matches', ['date', 'league'], unique=False)
    op.create_index(op.f('ix_matches_home_away'), 'matches', ['home_team', 'away_team'], unique=False)

    # Create predictions table
    op.create_table(
        'predictions',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('match_id', sa.Integer(), nullable=False),
        sa.Column('energy_score', sa.String(length=50), nullable=False),
        sa.Column('confidence', sa.String(length=50), nullable=False),
        sa.Column('predicted_winner', sa.String(length=255), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['match_id'], ['matches.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_predictions_id'), 'predictions', ['id'], unique=False)
    op.create_index(op.f('ix_predictions_match_id'), 'predictions', ['match_id'], unique=False)
    op.create_index(op.f('ix_predictions_created_at'), 'predictions', ['created_at'], unique=False)
    op.create_index(op.f('ix_predictions_confidence'), 'predictions', ['confidence'], unique=False)
    op.create_index(op.f('ix_predictions_match_id_created'), 'predictions', ['match_id', 'created_at'], unique=False)

    # Add foreign key to tweets table to match matches
    with op.batch_alter_table('tweets', schema=None) as batch_op:
        batch_op.create_foreign_key(
            'fk_tweets_match_id_matches',
            'matches',
            ['match_id'],
            ['id'],
            ondelete='CASCADE'
        )


def downgrade() -> None:
    """Drop matches and predictions tables."""
    # Drop predictions table
    op.drop_index(op.f('ix_predictions_match_id_created'), table_name='predictions')
    op.drop_index(op.f('ix_predictions_confidence'), table_name='predictions')
    op.drop_index(op.f('ix_predictions_created_at'), table_name='predictions')
    op.drop_index(op.f('ix_predictions_match_id'), table_name='predictions')
    op.drop_index(op.f('ix_predictions_id'), table_name='predictions')
    op.drop_table('predictions')

    # Drop matches table
    op.drop_index(op.f('ix_matches_home_away'), table_name='matches')
    op.drop_index(op.f('ix_matches_date_league'), table_name='matches')
    op.drop_index(op.f('ix_matches_status'), table_name='matches')
    op.drop_index(op.f('ix_matches_league'), table_name='matches')
    op.drop_index(op.f('ix_matches_date'), table_name='matches')
    op.drop_index(op.f('ix_matches_away_team'), table_name='matches')
    op.drop_index(op.f('ix_matches_home_team'), table_name='matches')
    op.drop_index(op.f('ix_matches_id'), table_name='matches')
    op.drop_table('matches')

    # Remove foreign key from tweets table
    with op.batch_alter_table('tweets', schema=None) as batch_op:
        batch_op.drop_constraint('fk_tweets_match_id_matches', type_='foreignkey')
