"""add idle_time_limit_in_seconds to org_configuration

Revision ID: h1234ij5678
Revises: g0123hi4567
Create Date: 2025-11-26 15:35:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """
    Add idle_time_limit_in_seconds column to org_configuration table
    """
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()
    
    if 'org_configuration' in tables:
        # Check if column already exists (for safety)
        columns = [col['name'] for col in inspector.get_columns('org_configuration')]
        if 'idle_time_limit_in_seconds' not in columns:
            op.add_column('org_configuration',
                sa.Column('idle_time_limit_in_seconds', sa.Integer(), nullable=True)
            )
            # Update existing rows to have default value (3600 seconds = 1 hour)
            op.execute("UPDATE org_configuration SET idle_time_limit_in_seconds = 3600 WHERE idle_time_limit_in_seconds IS NULL")
            print("Added 'idle_time_limit_in_seconds' column to 'org_configuration' table")


def downgrade() -> None:
    """
    Remove idle_time_limit_in_seconds column from org_configuration table
    """
    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 'idle_time_limit_in_seconds' in columns:
            op.drop_column('org_configuration', 'idle_time_limit_in_seconds')
            print("Removed 'idle_time_limit_in_seconds' column from 'org_configuration' table")

