"""Add marathon settings (is_public, game_proposal_mode)

Revision ID: 002_marathon_settings
Revises: 001_add_roles
Create Date: 2024-12-14

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

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


def column_exists(table_name: str, column_name: str) -> bool:
    bind = op.get_bind()
    inspector = inspect(bind)
    columns = [col['name'] for col in inspector.get_columns(table_name)]
    return column_name in columns


def upgrade() -> None:
    # Add is_public column to marathons table (default False = private)
    if not column_exists('marathons', 'is_public'):
        op.add_column('marathons', sa.Column('is_public', sa.Boolean(), nullable=False, server_default='false'))

    # Add game_proposal_mode column to marathons table
    # 'all_participants' - anyone can propose games (with moderation)
    # 'organizer_only' - only organizers can add games
    if not column_exists('marathons', 'game_proposal_mode'):
        op.add_column('marathons', sa.Column('game_proposal_mode', sa.String(20), nullable=False, server_default='all_participants'))


def downgrade() -> None:
    if column_exists('marathons', 'game_proposal_mode'):
        op.drop_column('marathons', 'game_proposal_mode')
    if column_exists('marathons', 'is_public'):
        op.drop_column('marathons', 'is_public')
