"""add timezone_preference to org_configuration

Revision ID: l5678no9012
Revises: k4567mn8901
Create Date: 2026-08-04 13:40:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "l5678no9012"
down_revision: Union[str, None] = "k4567mn8901"
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()

    if "org_configuration" in tables:
        columns = {col["name"] for col in inspector.get_columns("org_configuration")}
        if "timezone_preference" not in columns:
            op.add_column(
                "org_configuration",
                sa.Column("timezone_preference", sa.Integer(), nullable=True, server_default="1"),
            )
            op.execute(
                "UPDATE org_configuration SET timezone_preference = 1 "
                "WHERE timezone_preference IS NULL"
            )


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

    if "org_configuration" in tables:
        columns = {col["name"] for col in inspector.get_columns("org_configuration")}
        if "timezone_preference" in columns:
            op.drop_column("org_configuration", "timezone_preference")
