"""Fix datetime timezone

Revision ID: 7e00fa3c75c9
Revises: 001
Create Date: 2026-01-23 11:51:47.640123

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '7e00fa3c75c9'
down_revision: Union[str, None] = '001'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('attachments', 'uploaded_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('sessions', 'started_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('sessions', 'exported',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('false'))
    op.drop_index('idx_sessions_dates', table_name='sessions')
    op.drop_index('idx_sessions_tree', table_name='sessions')
    op.drop_index('idx_sessions_user', table_name='sessions')
    op.create_index(op.f('ix_sessions_completed_at'), 'sessions', ['completed_at'], unique=False)
    op.create_index(op.f('ix_sessions_started_at'), 'sessions', ['started_at'], unique=False)
    op.create_index(op.f('ix_sessions_tree_id'), 'sessions', ['tree_id'], unique=False)
    op.create_index(op.f('ix_sessions_user_id'), 'sessions', ['user_id'], unique=False)
    op.alter_column('teams', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('trees', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=False,
               existing_server_default=sa.text('true'))
    op.alter_column('trees', 'version',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('1'))
    op.alter_column('trees', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('trees', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('trees', 'usage_count',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.drop_index('idx_trees_category', table_name='trees')
    op.drop_index('idx_trees_search', table_name='trees', postgresql_using='gin')
    op.drop_index('idx_trees_team', table_name='trees')
    op.create_index(op.f('ix_trees_category'), 'trees', ['category'], unique=False)
    op.create_index(op.f('ix_trees_team_id'), 'trees', ['team_id'], unique=False)
    op.alter_column('users', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               type_=sa.DateTime(timezone=True),
               nullable=False,
               existing_server_default=sa.text('now()'))
    op.alter_column('users', 'last_login',
               existing_type=postgresql.TIMESTAMP(),
               type_=sa.DateTime(timezone=True),
               existing_nullable=True)
    op.drop_index('idx_users_email', table_name='users')
    op.drop_constraint('users_email_key', 'users', type_='unique')
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.create_unique_constraint('users_email_key', 'users', ['email'])
    op.create_index('idx_users_email', 'users', ['email'], unique=True)
    op.alter_column('users', 'last_login',
               existing_type=sa.DateTime(timezone=True),
               type_=postgresql.TIMESTAMP(),
               existing_nullable=True)
    op.alter_column('users', 'created_at',
               existing_type=sa.DateTime(timezone=True),
               type_=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.drop_index(op.f('ix_trees_team_id'), table_name='trees')
    op.drop_index(op.f('ix_trees_category'), table_name='trees')
    op.create_index('idx_trees_team', 'trees', ['team_id'], unique=False)
    op.create_index('idx_trees_search', 'trees', [sa.text("to_tsvector('english'::regconfig, (COALESCE(name, ''::character varying)::text || ' '::text) || COALESCE(description, ''::text))")], unique=False, postgresql_using='gin')
    op.create_index('idx_trees_category', 'trees', ['category'], unique=False)
    op.alter_column('trees', 'usage_count',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('trees', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('trees', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('trees', 'version',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('1'))
    op.alter_column('trees', 'is_active',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('true'))
    op.alter_column('teams', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.drop_index(op.f('ix_sessions_user_id'), table_name='sessions')
    op.drop_index(op.f('ix_sessions_tree_id'), table_name='sessions')
    op.drop_index(op.f('ix_sessions_started_at'), table_name='sessions')
    op.drop_index(op.f('ix_sessions_completed_at'), table_name='sessions')
    op.create_index('idx_sessions_user', 'sessions', ['user_id'], unique=False)
    op.create_index('idx_sessions_tree', 'sessions', ['tree_id'], unique=False)
    op.create_index('idx_sessions_dates', 'sessions', ['started_at', 'completed_at'], unique=False)
    op.alter_column('sessions', 'exported',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('false'))
    op.alter_column('sessions', 'started_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    op.alter_column('attachments', 'uploaded_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True,
               existing_server_default=sa.text('now()'))
    # ### end Alembic commands ###
