"""add has_on_demand_screenshot to activity_interval

Revision ID: j3456kl7891
Revises: i2345jk6789
Create Date: 2026-02-27 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """
    Add has_on_demand_screenshot column to activity_interval table.
    This flag tracks whether an on-demand screenshot was captured during the interval.
    """
    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 'has_on_demand_screenshot' not in columns:
            op.add_column('activity_interval',
                sa.Column('has_on_demand_screenshot', sa.Boolean(), nullable=False, server_default=sa.false())
            )
            op.execute("UPDATE activity_interval SET has_on_demand_screenshot = 0 WHERE has_on_demand_screenshot IS NULL")
            print("Added 'has_on_demand_screenshot' column to 'activity_interval' table")


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