"""Add disputes tables for proof verification system

Revision ID: 009_add_disputes
Revises: 008_rename_to_game_choice
Create Date: 2024-12-16

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '009_add_disputes'
down_revision: Union[str, None] = '008_rename_to_game_choice'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()

    # Create disputes table
    if 'disputes' not in tables:
        op.create_table(
            'disputes',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('assignment_id', sa.Integer(), nullable=False),
            sa.Column('raised_by_id', sa.Integer(), nullable=False),
            sa.Column('reason', sa.Text(), nullable=False),
            sa.Column('status', sa.String(20), nullable=False, server_default='open'),
            sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()),
            sa.Column('resolved_at', sa.DateTime(), nullable=True),
            sa.PrimaryKeyConstraint('id'),
            sa.ForeignKeyConstraint(['assignment_id'], ['assignments.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['raised_by_id'], ['users.id'], ondelete='CASCADE'),
            sa.UniqueConstraint('assignment_id', name='uq_dispute_assignment'),
        )
        op.create_index('ix_disputes_assignment_id', 'disputes', ['assignment_id'])

    # Create dispute_comments table
    if 'dispute_comments' not in tables:
        op.create_table(
            'dispute_comments',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('dispute_id', sa.Integer(), nullable=False),
            sa.Column('user_id', sa.Integer(), nullable=False),
            sa.Column('text', sa.Text(), nullable=False),
            sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()),
            sa.PrimaryKeyConstraint('id'),
            sa.ForeignKeyConstraint(['dispute_id'], ['disputes.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
        )
        op.create_index('ix_dispute_comments_dispute_id', 'dispute_comments', ['dispute_id'])

    # Create dispute_votes table
    if 'dispute_votes' not in tables:
        op.create_table(
            'dispute_votes',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('dispute_id', sa.Integer(), nullable=False),
            sa.Column('user_id', sa.Integer(), nullable=False),
            sa.Column('vote', sa.Boolean(), nullable=False),
            sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()),
            sa.PrimaryKeyConstraint('id'),
            sa.ForeignKeyConstraint(['dispute_id'], ['disputes.id'], ondelete='CASCADE'),
            sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
            sa.UniqueConstraint('dispute_id', 'user_id', name='uq_dispute_vote_user'),
        )
        op.create_index('ix_dispute_votes_dispute_id', 'dispute_votes', ['dispute_id'])


def downgrade() -> None:
    op.drop_index('ix_dispute_votes_dispute_id', table_name='dispute_votes')
    op.drop_table('dispute_votes')
    op.drop_index('ix_dispute_comments_dispute_id', table_name='dispute_comments')
    op.drop_table('dispute_comments')
    op.drop_index('ix_disputes_assignment_id', table_name='disputes')
    op.drop_table('disputes')
