"""Add position to Item model for reordering

Revision ID: 91d00c100f5b
Revises: 0001_initial_schema
Create Date: 2025-06-07 14:59:48.761124

"""
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 = '91d00c100f5b'
down_revision: Union[str, None] = '0001_initial_schema'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_apscheduler_jobs_next_run_time', table_name='apscheduler_jobs')
    op.drop_table('apscheduler_jobs')
    op.alter_column('chore_assignments', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('expenses', 'currency',
               existing_type=sa.VARCHAR(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('expenses', 'is_recurring',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.drop_index('ix_expenses_recurring_next_occurrence', table_name='expenses', postgresql_where='(is_recurring = true)')
    op.alter_column('invites', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.add_column('items', sa.Column('position', sa.Integer(), server_default='0', nullable=False))
    op.alter_column('items', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.create_index('ix_items_list_id_position', 'items', ['list_id', 'position'], unique=False)
    op.alter_column('lists', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('recurrence_patterns', 'interval',
               existing_type=sa.INTEGER(),
               server_default=None,
               existing_nullable=False)
    op.create_index(op.f('ix_settlement_activities_id'), 'settlement_activities', ['id'], unique=False)
    op.create_index('ix_settlement_activity_created_by_user_id', 'settlement_activities', ['created_by_user_id'], unique=False)
    op.create_index('ix_settlement_activity_expense_split_id', 'settlement_activities', ['expense_split_id'], unique=False)
    op.create_index('ix_settlement_activity_paid_by_user_id', 'settlement_activities', ['paid_by_user_id'], unique=False)
    op.alter_column('users', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('users', 'is_superuser',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    op.alter_column('users', 'is_verified',
               existing_type=sa.BOOLEAN(),
               server_default=None,
               existing_nullable=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('users', 'is_verified',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.alter_column('users', 'is_superuser',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.alter_column('users', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('true'),
               existing_nullable=False)
    op.drop_index('ix_settlement_activity_paid_by_user_id', table_name='settlement_activities')
    op.drop_index('ix_settlement_activity_expense_split_id', table_name='settlement_activities')
    op.drop_index('ix_settlement_activity_created_by_user_id', table_name='settlement_activities')
    op.drop_index(op.f('ix_settlement_activities_id'), table_name='settlement_activities')
    op.alter_column('recurrence_patterns', 'interval',
               existing_type=sa.INTEGER(),
               server_default=sa.text('1'),
               existing_nullable=False)
    op.alter_column('lists', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.drop_index('ix_items_list_id_position', table_name='items')
    op.alter_column('items', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.drop_column('items', 'position')
    op.alter_column('invites', 'is_active',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('true'),
               existing_nullable=False)
    op.create_index('ix_expenses_recurring_next_occurrence', 'expenses', ['is_recurring', 'next_occurrence'], unique=False, postgresql_where='(is_recurring = true)')
    op.alter_column('expenses', 'is_recurring',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.alter_column('expenses', 'currency',
               existing_type=sa.VARCHAR(),
               server_default=sa.text("'USD'::character varying"),
               existing_nullable=False)
    op.alter_column('chore_assignments', 'is_complete',
               existing_type=sa.BOOLEAN(),
               server_default=sa.text('false'),
               existing_nullable=False)
    op.create_table('apscheduler_jobs',
    sa.Column('id', sa.VARCHAR(length=191), autoincrement=False, nullable=False),
    sa.Column('next_run_time', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True),
    sa.Column('job_state', postgresql.BYTEA(), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('id', name='apscheduler_jobs_pkey')
    )
    op.create_index('ix_apscheduler_jobs_next_run_time', 'apscheduler_jobs', ['next_run_time'], unique=False)
    # ### end Alembic commands ###
