from alembic import op
import sqlalchemy as sa

revision = '0006_update_calendar_entries'
down_revision = '0005_add_description'  # use the appropriate previous revision ID
branch_labels = None
depends_on = None

def upgrade():
    # Add user_id and session_id columns
    op.add_column('calendar_entries', sa.Column('user_id', sa.Integer(), nullable=True))
    op.create_foreign_key('fk_calendar_entries_user_id', 'calendar_entries', 'users', ['user_id'], ['id'])
    op.add_column('calendar_entries', sa.Column('session_id', sa.String(length=128), nullable=True))
    # Add state and cost columns
    op.add_column('calendar_entries', sa.Column('state', sa.String(length=20), nullable=False, server_default='pending'))
    op.add_column('calendar_entries', sa.Column('cost', sa.Numeric(10,2), nullable=False, server_default='10'))
    # (Optional) Create indexes on the new columns
    op.create_index('ix_calendar_entries_user_id', 'calendar_entries', ['user_id'])
    op.create_index('ix_calendar_entries_session_id', 'calendar_entries', ['session_id'])

def downgrade():
    op.drop_index('ix_calendar_entries_session_id', table_name='calendar_entries')
    op.drop_index('ix_calendar_entries_user_id', table_name='calendar_entries')
    op.drop_column('calendar_entries', 'cost')
    op.drop_column('calendar_entries', 'state')
    op.drop_column('calendar_entries', 'session_id')
    op.drop_constraint('fk_calendar_entries_user_id', 'calendar_entries', type_='foreignkey')
    op.drop_column('calendar_entries', 'user_id')
