"""add unit_system column to User

Revision ID: 68875bccab07
Revises: 5e03be0b5d05
Create Date: 2025-03-04 10:29:27.575449

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector


# revision identifiers, used by Alembic.
revision: str = '68875bccab07'
down_revision: Union[str, None] = '5e03be0b5d05'
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 = Inspector.from_engine(bind)
    columns = [col['name'] for col in inspector.get_columns(table_name)]
    return column_name in columns


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    if not column_exists('users', 'unit_system'):
        op.add_column('users', sa.Column('unit_system', sa.String(), nullable=True))
    
    if op.get_bind().dialect.name == 'sqlite':
        op.execute("ALTER TABLE users RENAME COLUMN username TO username_old")
        op.execute("""
            ALTER TABLE users ADD COLUMN username VARCHAR(50) DEFAULT 'User' NOT NULL
        """)
        op.execute("""
            UPDATE users SET username = username_old WHERE username IS NULL
        """)
        op.execute("""
            ALTER TABLE users DROP COLUMN username_old
        """)
    else:
        op.alter_column('users', 'username',
                        existing_type=sa.VARCHAR(length=50),
                        nullable=False,
                        existing_server_default=sa.text("'User'"))
    
    if column_exists('users', 'precipitation_unit'):
        op.drop_column('users', 'precipitation_unit')
    if column_exists('users', 'wind_speed_unit'):
        op.drop_column('users', 'wind_speed_unit')
    if column_exists('users', 'temperature_unit'):
        op.drop_column('users', 'temperature_unit')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    if not column_exists('users', 'temperature_unit'):
        op.add_column('users', sa.Column('temperature_unit', sa.VARCHAR(), nullable=True))
    if not column_exists('users', 'wind_speed_unit'):
        op.add_column('users', sa.Column('wind_speed_unit', sa.VARCHAR(), nullable=True))
    if not column_exists('users', 'precipitation_unit'):
        op.add_column('users', sa.Column('precipitation_unit', sa.VARCHAR(), nullable=True))
    
    if op.get_bind().dialect.name == 'sqlite':
        op.execute("ALTER TABLE users RENAME COLUMN username TO username_old")
        op.execute("""
            ALTER TABLE users ADD COLUMN username VARCHAR(50)
        """)
        op.execute("""
            UPDATE users SET username = username_old WHERE username IS NULL
        """)
        op.execute("""
            ALTER TABLE users DROP COLUMN username_old
        """)
    else:
        op.alter_column('users', 'username',
                        existing_type=sa.VARCHAR(length=50),
                        nullable=True,
                        existing_server_default=sa.text("'User'"))
    
    if column_exists('users', 'unit_system'):
        op.drop_column('users', 'unit_system')
    # ### end Alembic commands ###