"""add is_idle to activity_interval

Revision ID: i2345jk6789
Revises: h1234ij5678
Create Date: 2025-01-15 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """
    Add is_idle column to activity_interval table
    """
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()
    
    if 'activity_interval' in tables:
        # Check if column already exists (for safety)
        columns = [col['name'] for col in inspector.get_columns('activity_interval')]
        if 'is_idle' not in columns:
            op.add_column('activity_interval',
                sa.Column('is_idle', sa.Boolean(), nullable=False, server_default=sa.false())
            )
            # Update existing rows to have default value (False) - this should not be needed due to server_default, but keeping for safety
            op.execute("UPDATE activity_interval SET is_idle = 0 WHERE is_idle IS NULL")
            print("Added 'is_idle' column to 'activity_interval' table")


def downgrade() -> None:
    """
    Remove is_idle column from activity_interval table
    """
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()
    
    if 'activity_interval' in tables:
        columns = [col['name'] for col in inspector.get_columns('activity_interval')]
        if 'is_idle' in columns:
            op.drop_column('activity_interval', 'is_idle')
            print("Removed 'is_idle' column from 'activity_interval' table")

