"""rename appmonitor and orgconfiguration tables

Revision ID: d7890ef1234
Revises: c7890abc1234
Create Date: 2025-01-17 12:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """
    Rename Appmonitor table to app_monitor
    Rename orgconfiguration table to org_configuration
    """
    # Check if tables exist
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()
    
    # Rename Appmonitor to app_monitor
    if 'Appmonitor' in tables:
        op.rename_table('Appmonitor', 'app_monitor')
    elif 'app_monitor' not in tables:
        # Table doesn't exist, create it fresh (shouldn't happen, but safety check)
        op.create_table('app_monitor',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('spendTime', sa.Integer(), nullable=False),
            sa.Column('active_count', sa.Integer(), nullable=False),
            sa.Column('name', sa.String(), nullable=False),
            sa.Column('url', sa.String(), nullable=True),
            sa.Column('isApp', sa.Boolean(), nullable=True),
            sa.Column('timestamp', sa.DateTime(), nullable=True),
            sa.Column('isUploaded', sa.Boolean(), nullable=True),
            sa.Column('counter', sa.Integer(), nullable=False),
            sa.Column('timezone', sa.Boolean(), nullable=True),
            sa.PrimaryKeyConstraint('id')
        )
    
    # Rename orgconfiguration to org_configuration
    if 'orgconfiguration' in tables:
        op.rename_table('orgconfiguration', 'org_configuration')
    elif 'org_configuration' not in tables:
        # Table doesn't exist, create it fresh (shouldn't happen, but safety check)
        op.create_table('org_configuration',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('org_id', sa.Integer(), nullable=True),
            sa.Column('user_id', sa.Integer(), nullable=True),
            sa.Column('screenshot_interval', sa.Integer(), nullable=True),
            sa.Column('date', sa.String(), nullable=True),
            sa.Column('appandurlstatus', sa.Integer(), nullable=True),
            sa.Column('ssfrequencystatus', sa.Integer(), nullable=True),
            sa.Column('screenshotblurstatus', sa.Integer(), nullable=True),
            sa.Column('livescreenshotstatus', sa.Integer(), nullable=True),
            sa.Column('idlenotes_status', sa.Integer(), nullable=True),
            sa.Column('break_limit', sa.String(), nullable=True),
            sa.Column('break_status', sa.Integer(), nullable=True),
            sa.Column('notes_char_limit', sa.String(), nullable=True),
            sa.Column('idle_limit', sa.String(), nullable=True),
            sa.Column('can_delete_scrnshots', sa.Integer(), nullable=True),
            sa.Column('restrict_timer_to_jobsite', sa.Integer(), nullable=True),
            sa.Column('todos_files_upload_limit', sa.Integer(), nullable=True),
            sa.Column('todo_file_size_limit', sa.Integer(), nullable=True),
            sa.Column('org_timezone', sa.String(), nullable=True),
            sa.Column('user_timezone', sa.String(), nullable=True),
            sa.Column('user_timezone_gmt', sa.String(), nullable=True),
            sa.Column('user_timezone_gmt_dst', sa.String(), nullable=True),
            sa.Column('user_timezone_gmt_shortcode', sa.String(), nullable=True),
            sa.Column('bulkInvite', sa.Boolean(), nullable=True),
            sa.Column('timeInterval', sa.String(), nullable=True),
            sa.Column('blur_intensity', sa.Integer(), nullable=True),
            sa.Column('img_quality', sa.Integer(), nullable=True),
            sa.Column('notification_status', sa.Integer(), nullable=True),
            sa.Column('timestamp', sa.DateTime(), nullable=True),
            sa.PrimaryKeyConstraint('id')
        )


def downgrade() -> None:
    """
    Revert app_monitor table back to Appmonitor
    Revert org_configuration table back to orgconfiguration
    """
    conn = op.get_bind()
    inspector = sa.inspect(conn)
    tables = inspector.get_table_names()
    
    # Rename app_monitor back to Appmonitor
    if 'app_monitor' in tables:
        op.rename_table('app_monitor', 'Appmonitor')
    
    # Rename org_configuration back to orgconfiguration
    if 'org_configuration' in tables:
        op.rename_table('org_configuration', 'orgconfiguration')

