"""Add notification settings to users

Revision ID: 022_add_notification_settings
Revises: 021_add_bonus_disputes
Create Date: 2025-01-04

"""
from typing import Sequence, Union

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


# revision identifiers, used by Alembic.
revision: str = '022_add_notification_settings'
down_revision: Union[str, None] = '021_add_bonus_disputes'
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 notification settings (all enabled by default)
    if not column_exists('users', 'notify_events'):
        op.add_column('users', sa.Column('notify_events', sa.Boolean(), server_default='true', nullable=False))
    if not column_exists('users', 'notify_disputes'):
        op.add_column('users', sa.Column('notify_disputes', sa.Boolean(), server_default='true', nullable=False))
    if not column_exists('users', 'notify_moderation'):
        op.add_column('users', sa.Column('notify_moderation', sa.Boolean(), server_default='true', nullable=False))


def downgrade() -> None:
    if column_exists('users', 'notify_moderation'):
        op.drop_column('users', 'notify_moderation')
    if column_exists('users', 'notify_disputes'):
        op.drop_column('users', 'notify_disputes')
    if column_exists('users', 'notify_events'):
        op.drop_column('users', 'notify_events')
