"""add public.users.timezone for per-user display timezone

All timestamps stay stored as naive UTC; this column only controls how they
are rendered. NULL means "inherit the org timezone" (organizations.timezone,
itself defaulting to UTC), so existing users need no backfill.

Revision ID: tz001
Revises: gcd001
Create Date: 2026-08-07
"""
from alembic import op
import sqlalchemy as sa

revision = 'tz001'
down_revision = 'gcd001'
branch_labels = None
depends_on = None


def _public_column_exists(conn, table, column):
    return conn.execute(sa.text(
        "SELECT 1 FROM information_schema.columns "
        "WHERE table_schema='public' AND table_name=:t AND column_name=:c"
    ), {"t": table, "c": column}).fetchone() is not None


def upgrade():
    conn = op.get_bind()
    if _public_column_exists(conn, 'users', 'timezone'):
        return
    op.add_column('users', sa.Column('timezone', sa.String(50), nullable=True),
                  schema='public')


def downgrade():
    op.drop_column('users', 'timezone', schema='public')
