"""Add telegram profile fields to users

Revision ID: 010_add_telegram_profile
Revises: 009_add_disputes
Create Date: 2024-12-16

"""
from typing import Sequence, Union

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


# revision identifiers, used by Alembic.
revision: str = '010_add_telegram_profile'
down_revision: Union[str, None] = '009_add_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:
    if not column_exists('users', 'telegram_first_name'):
        op.add_column('users', sa.Column('telegram_first_name', sa.String(100), nullable=True))
    if not column_exists('users', 'telegram_last_name'):
        op.add_column('users', sa.Column('telegram_last_name', sa.String(100), nullable=True))
    if not column_exists('users', 'telegram_avatar_url'):
        op.add_column('users', sa.Column('telegram_avatar_url', sa.String(500), nullable=True))


def downgrade() -> None:
    if column_exists('users', 'telegram_avatar_url'):
        op.drop_column('users', 'telegram_avatar_url')
    if column_exists('users', 'telegram_last_name'):
        op.drop_column('users', 'telegram_last_name')
    if column_exists('users', 'telegram_first_name'):
        op.drop_column('users', 'telegram_first_name')
