from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
revision = '59baf2338f8a'
down_revision = None
branch_labels = None
depends_on = None

def upgrade() -> None:
    op.create_table('audit_logs', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('user_id', sa.Integer(), nullable=True), sa.Column('action', sa.String(length=100), nullable=False), sa.Column('resource_type', sa.String(length=50), nullable=False), sa.Column('resource_id', sa.Integer(), nullable=True), sa.Column('ip_address', sa.String(length=45), nullable=True), sa.Column('user_agent', sa.String(length=255), nullable=True), sa.Column('request_id', sa.String(length=36), nullable=True), sa.Column('details', sa.JSON(), nullable=True), sa.Column('status', sa.String(length=20), nullable=False), sa.Column('error_message', sa.Text(), nullable=True), sa.Column('created_at', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['user_id'], ['users.id']), sa.PrimaryKeyConstraint('id'))
    op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'], unique=False)
    op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'], unique=False)
    op.create_index(op.f('ix_audit_logs_id'), 'audit_logs', ['id'], unique=False)
    op.create_index(op.f('ix_audit_logs_request_id'), 'audit_logs', ['request_id'], unique=False)
    op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'], unique=False)
    op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'], unique=False)
    op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'], unique=False)
    op.drop_index('name', table_name='SequelizeMeta')
    op.drop_table('SequelizeMeta')
    op.drop_index('banners_is_active', table_name='banners')
    op.drop_index('banners_position', table_name='banners')
    op.create_index(op.f('ix_banners_id'), 'banners', ['id'], unique=False)
    op.drop_constraint('bookings_ibfk_2', 'bookings', type_='foreignkey')
    op.drop_constraint('bookings_ibfk_1', 'bookings', type_='foreignkey')
    op.drop_index('booking_number', table_name='bookings')
    op.drop_index('bookings_booking_number', table_name='bookings')
    op.drop_index('bookings_check_in_date', table_name='bookings')
    op.drop_index('bookings_check_out_date', table_name='bookings')
    op.drop_index('bookings_room_id', table_name='bookings')
    op.drop_index('bookings_status', table_name='bookings')
    op.drop_index('bookings_user_id', table_name='bookings')
    op.create_index(op.f('ix_bookings_booking_number'), 'bookings', ['booking_number'], unique=True)
    op.create_index(op.f('ix_bookings_id'), 'bookings', ['id'], unique=False)
    op.create_foreign_key(None, 'bookings', 'users', ['user_id'], ['id'])
    op.create_foreign_key(None, 'bookings', 'rooms', ['room_id'], ['id'])
    op.drop_constraint('checkin_checkout_ibfk_1', 'checkin_checkout', type_='foreignkey')
    op.drop_constraint('checkin_checkout_ibfk_2', 'checkin_checkout', type_='foreignkey')
    op.drop_constraint('checkin_checkout_ibfk_3', 'checkin_checkout', type_='foreignkey')
    op.drop_index('checkin_checkout_booking_id', table_name='checkin_checkout')
    op.create_index(op.f('ix_checkin_checkout_id'), 'checkin_checkout', ['id'], unique=False)
    op.create_unique_constraint(None, 'checkin_checkout', ['booking_id'])
    op.create_foreign_key(None, 'checkin_checkout', 'bookings', ['booking_id'], ['id'])
    op.create_foreign_key(None, 'checkin_checkout', 'users', ['checkout_by'], ['id'])
    op.create_foreign_key(None, 'checkin_checkout', 'users', ['checkin_by'], ['id'])
    op.drop_constraint('favorites_ibfk_2', 'favorites', type_='foreignkey')
    op.drop_constraint('favorites_ibfk_1', 'favorites', type_='foreignkey')
    op.drop_index('favorites_room_id', table_name='favorites')
    op.drop_index('favorites_user_id', table_name='favorites')
    op.drop_index('unique_user_room_favorite', table_name='favorites')
    op.create_index(op.f('ix_favorites_id'), 'favorites', ['id'], unique=False)
    op.create_foreign_key(None, 'favorites', 'users', ['user_id'], ['id'])
    op.create_foreign_key(None, 'favorites', 'rooms', ['room_id'], ['id'])
    op.alter_column('password_reset_tokens', 'used', existing_type=mysql.TINYINT(display_width=1), nullable=False, existing_server_default=sa.text("'0'"))
    op.drop_constraint('password_reset_tokens_ibfk_1', 'password_reset_tokens', type_='foreignkey')
    op.drop_index('password_reset_tokens_token', table_name='password_reset_tokens')
    op.drop_index('password_reset_tokens_user_id', table_name='password_reset_tokens')
    op.drop_index('token', table_name='password_reset_tokens')
    op.create_index(op.f('ix_password_reset_tokens_id'), 'password_reset_tokens', ['id'], unique=False)
    op.create_index(op.f('ix_password_reset_tokens_token'), 'password_reset_tokens', ['token'], unique=True)
    op.create_foreign_key(None, 'password_reset_tokens', 'users', ['user_id'], ['id'])
    op.alter_column('payments', 'deposit_percentage', existing_type=mysql.INTEGER(), comment=None, existing_comment='Percentage of deposit (e.g., 20, 30, 50)', existing_nullable=True)
    op.drop_constraint('payments_related_payment_id_foreign_idx', 'payments', type_='foreignkey')
    op.drop_constraint('payments_ibfk_1', 'payments', type_='foreignkey')
    op.drop_index('payments_booking_id', table_name='payments')
    op.drop_index('payments_payment_status', table_name='payments')
    op.create_index(op.f('ix_payments_id'), 'payments', ['id'], unique=False)
    op.create_foreign_key(None, 'payments', 'bookings', ['booking_id'], ['id'])
    op.create_foreign_key(None, 'payments', 'payments', ['related_payment_id'], ['id'])
    op.drop_index('code', table_name='promotions')
    op.drop_index('promotions_code', table_name='promotions')
    op.drop_index('promotions_is_active', table_name='promotions')
    op.create_index(op.f('ix_promotions_code'), 'promotions', ['code'], unique=True)
    op.create_index(op.f('ix_promotions_id'), 'promotions', ['id'], unique=False)
    op.drop_constraint('refresh_tokens_ibfk_1', 'refresh_tokens', type_='foreignkey')
    op.drop_index('refresh_tokens_token', table_name='refresh_tokens')
    op.drop_index('refresh_tokens_user_id', table_name='refresh_tokens')
    op.drop_index('token', table_name='refresh_tokens')
    op.create_index(op.f('ix_refresh_tokens_id'), 'refresh_tokens', ['id'], unique=False)
    op.create_index(op.f('ix_refresh_tokens_token'), 'refresh_tokens', ['token'], unique=True)
    op.create_foreign_key(None, 'refresh_tokens', 'users', ['user_id'], ['id'])
    op.drop_constraint('reviews_ibfk_2', 'reviews', type_='foreignkey')
    op.drop_constraint('reviews_ibfk_1', 'reviews', type_='foreignkey')
    op.drop_index('reviews_room_id', table_name='reviews')
    op.drop_index('reviews_status', table_name='reviews')
    op.drop_index('reviews_user_id', table_name='reviews')
    op.create_index(op.f('ix_reviews_id'), 'reviews', ['id'], unique=False)
    op.create_foreign_key(None, 'reviews', 'rooms', ['room_id'], ['id'])
    op.create_foreign_key(None, 'reviews', 'users', ['user_id'], ['id'])
    op.drop_index('name', table_name='roles')
    op.create_index(op.f('ix_roles_id'), 'roles', ['id'], unique=False)
    op.create_index(op.f('ix_roles_name'), 'roles', ['name'], unique=True)
    op.create_index(op.f('ix_room_types_id'), 'room_types', ['id'], unique=False)
    op.drop_constraint('rooms_ibfk_1', 'rooms', type_='foreignkey')
    op.drop_index('room_number', table_name='rooms')
    op.drop_index('rooms_featured', table_name='rooms')
    op.drop_index('rooms_room_type_id', table_name='rooms')
    op.drop_index('rooms_status', table_name='rooms')
    op.create_index(op.f('ix_rooms_id'), 'rooms', ['id'], unique=False)
    op.create_index(op.f('ix_rooms_room_number'), 'rooms', ['room_number'], unique=True)
    op.create_foreign_key(None, 'rooms', 'room_types', ['room_type_id'], ['id'])
    op.drop_constraint('service_usages_ibfk_1', 'service_usages', type_='foreignkey')
    op.drop_constraint('service_usages_ibfk_2', 'service_usages', type_='foreignkey')
    op.drop_index('service_usages_booking_id', table_name='service_usages')
    op.drop_index('service_usages_service_id', table_name='service_usages')
    op.create_index(op.f('ix_service_usages_id'), 'service_usages', ['id'], unique=False)
    op.create_foreign_key(None, 'service_usages', 'bookings', ['booking_id'], ['id'])
    op.create_foreign_key(None, 'service_usages', 'services', ['service_id'], ['id'])
    op.drop_index('services_category', table_name='services')
    op.create_index(op.f('ix_services_id'), 'services', ['id'], unique=False)
    op.drop_constraint('users_ibfk_1', 'users', type_='foreignkey')
    op.drop_index('email', table_name='users')
    op.drop_index('users_email', table_name='users')
    op.drop_index('users_role_id', table_name='users')
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
    op.create_foreign_key(None, 'users', 'roles', ['role_id'], ['id'])

def downgrade() -> None:
    op.drop_constraint(None, 'users', type_='foreignkey')
    op.create_foreign_key('users_ibfk_1', 'users', 'roles', ['role_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.drop_index(op.f('ix_users_id'), table_name='users')
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.create_index('users_role_id', 'users', ['role_id'], unique=False)
    op.create_index('users_email', 'users', ['email'], unique=False)
    op.create_index('email', 'users', ['email'], unique=False)
    op.drop_index(op.f('ix_services_id'), table_name='services')
    op.create_index('services_category', 'services', ['category'], unique=False)
    op.drop_constraint(None, 'service_usages', type_='foreignkey')
    op.drop_constraint(None, 'service_usages', type_='foreignkey')
    op.create_foreign_key('service_usages_ibfk_2', 'service_usages', 'services', ['service_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.create_foreign_key('service_usages_ibfk_1', 'service_usages', 'bookings', ['booking_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.drop_index(op.f('ix_service_usages_id'), table_name='service_usages')
    op.create_index('service_usages_service_id', 'service_usages', ['service_id'], unique=False)
    op.create_index('service_usages_booking_id', 'service_usages', ['booking_id'], unique=False)
    op.drop_constraint(None, 'rooms', type_='foreignkey')
    op.create_foreign_key('rooms_ibfk_1', 'rooms', 'room_types', ['room_type_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.drop_index(op.f('ix_rooms_room_number'), table_name='rooms')
    op.drop_index(op.f('ix_rooms_id'), table_name='rooms')
    op.create_index('rooms_status', 'rooms', ['status'], unique=False)
    op.create_index('rooms_room_type_id', 'rooms', ['room_type_id'], unique=False)
    op.create_index('rooms_featured', 'rooms', ['featured'], unique=False)
    op.create_index('room_number', 'rooms', ['room_number'], unique=False)
    op.drop_index(op.f('ix_room_types_id'), table_name='room_types')
    op.drop_index(op.f('ix_roles_name'), table_name='roles')
    op.drop_index(op.f('ix_roles_id'), table_name='roles')
    op.create_index('name', 'roles', ['name'], unique=False)
    op.drop_constraint(None, 'reviews', type_='foreignkey')
    op.drop_constraint(None, 'reviews', type_='foreignkey')
    op.create_foreign_key('reviews_ibfk_1', 'reviews', 'users', ['user_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.create_foreign_key('reviews_ibfk_2', 'reviews', 'rooms', ['room_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.drop_index(op.f('ix_reviews_id'), table_name='reviews')
    op.create_index('reviews_user_id', 'reviews', ['user_id'], unique=False)
    op.create_index('reviews_status', 'reviews', ['status'], unique=False)
    op.create_index('reviews_room_id', 'reviews', ['room_id'], unique=False)
    op.drop_constraint(None, 'refresh_tokens', type_='foreignkey')
    op.create_foreign_key('refresh_tokens_ibfk_1', 'refresh_tokens', 'users', ['user_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.drop_index(op.f('ix_refresh_tokens_token'), table_name='refresh_tokens')
    op.drop_index(op.f('ix_refresh_tokens_id'), table_name='refresh_tokens')
    op.create_index('token', 'refresh_tokens', ['token'], unique=False)
    op.create_index('refresh_tokens_user_id', 'refresh_tokens', ['user_id'], unique=False)
    op.create_index('refresh_tokens_token', 'refresh_tokens', ['token'], unique=False)
    op.drop_index(op.f('ix_promotions_id'), table_name='promotions')
    op.drop_index(op.f('ix_promotions_code'), table_name='promotions')
    op.create_index('promotions_is_active', 'promotions', ['is_active'], unique=False)
    op.create_index('promotions_code', 'promotions', ['code'], unique=False)
    op.create_index('code', 'promotions', ['code'], unique=False)
    op.drop_constraint(None, 'payments', type_='foreignkey')
    op.drop_constraint(None, 'payments', type_='foreignkey')
    op.create_foreign_key('payments_ibfk_1', 'payments', 'bookings', ['booking_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.create_foreign_key('payments_related_payment_id_foreign_idx', 'payments', 'payments', ['related_payment_id'], ['id'], onupdate='CASCADE', ondelete='SET NULL')
    op.drop_index(op.f('ix_payments_id'), table_name='payments')
    op.create_index('payments_payment_status', 'payments', ['payment_status'], unique=False)
    op.create_index('payments_booking_id', 'payments', ['booking_id'], unique=False)
    op.alter_column('payments', 'deposit_percentage', existing_type=mysql.INTEGER(), comment='Percentage of deposit (e.g., 20, 30, 50)', existing_nullable=True)
    op.drop_constraint(None, 'password_reset_tokens', type_='foreignkey')
    op.create_foreign_key('password_reset_tokens_ibfk_1', 'password_reset_tokens', 'users', ['user_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.drop_index(op.f('ix_password_reset_tokens_token'), table_name='password_reset_tokens')
    op.drop_index(op.f('ix_password_reset_tokens_id'), table_name='password_reset_tokens')
    op.create_index('token', 'password_reset_tokens', ['token'], unique=False)
    op.create_index('password_reset_tokens_user_id', 'password_reset_tokens', ['user_id'], unique=False)
    op.create_index('password_reset_tokens_token', 'password_reset_tokens', ['token'], unique=False)
    op.alter_column('password_reset_tokens', 'used', existing_type=mysql.TINYINT(display_width=1), nullable=True, existing_server_default=sa.text("'0'"))
    op.drop_constraint(None, 'favorites', type_='foreignkey')
    op.drop_constraint(None, 'favorites', type_='foreignkey')
    op.create_foreign_key('favorites_ibfk_1', 'favorites', 'users', ['user_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.create_foreign_key('favorites_ibfk_2', 'favorites', 'rooms', ['room_id'], ['id'], onupdate='CASCADE', ondelete='CASCADE')
    op.drop_index(op.f('ix_favorites_id'), table_name='favorites')
    op.create_index('unique_user_room_favorite', 'favorites', ['user_id', 'room_id'], unique=False)
    op.create_index('favorites_user_id', 'favorites', ['user_id'], unique=False)
    op.create_index('favorites_room_id', 'favorites', ['room_id'], unique=False)
    op.drop_constraint(None, 'checkin_checkout', type_='foreignkey')
    op.drop_constraint(None, 'checkin_checkout', type_='foreignkey')
    op.drop_constraint(None, 'checkin_checkout', type_='foreignkey')
    op.create_foreign_key('checkin_checkout_ibfk_3', 'checkin_checkout', 'users', ['checkout_by'], ['id'], onupdate='CASCADE', ondelete='SET NULL')
    op.create_foreign_key('checkin_checkout_ibfk_2', 'checkin_checkout', 'users', ['checkin_by'], ['id'], onupdate='CASCADE', ondelete='SET NULL')
    op.create_foreign_key('checkin_checkout_ibfk_1', 'checkin_checkout', 'bookings', ['booking_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.drop_constraint(None, 'checkin_checkout', type_='unique')
    op.drop_index(op.f('ix_checkin_checkout_id'), table_name='checkin_checkout')
    op.create_index('checkin_checkout_booking_id', 'checkin_checkout', ['booking_id'], unique=False)
    op.drop_constraint(None, 'bookings', type_='foreignkey')
    op.drop_constraint(None, 'bookings', type_='foreignkey')
    op.create_foreign_key('bookings_ibfk_1', 'bookings', 'users', ['user_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.create_foreign_key('bookings_ibfk_2', 'bookings', 'rooms', ['room_id'], ['id'], onupdate='CASCADE', ondelete='RESTRICT')
    op.drop_index(op.f('ix_bookings_id'), table_name='bookings')
    op.drop_index(op.f('ix_bookings_booking_number'), table_name='bookings')
    op.create_index('bookings_user_id', 'bookings', ['user_id'], unique=False)
    op.create_index('bookings_status', 'bookings', ['status'], unique=False)
    op.create_index('bookings_room_id', 'bookings', ['room_id'], unique=False)
    op.create_index('bookings_check_out_date', 'bookings', ['check_out_date'], unique=False)
    op.create_index('bookings_check_in_date', 'bookings', ['check_in_date'], unique=False)
    op.create_index('bookings_booking_number', 'bookings', ['booking_number'], unique=False)
    op.create_index('booking_number', 'bookings', ['booking_number'], unique=False)
    op.drop_index(op.f('ix_banners_id'), table_name='banners')
    op.create_index('banners_position', 'banners', ['position'], unique=False)
    op.create_index('banners_is_active', 'banners', ['is_active'], unique=False)
    op.create_table('SequelizeMeta', sa.Column('name', mysql.VARCHAR(collation='utf8mb3_unicode_ci', length=255), nullable=False), sa.PrimaryKeyConstraint('name'), mysql_collate='utf8mb3_unicode_ci', mysql_default_charset='utf8mb3', mysql_engine='InnoDB')
    op.create_index('name', 'SequelizeMeta', ['name'], unique=False)
    op.drop_index(op.f('ix_audit_logs_user_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_resource_type'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_resource_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_request_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
    op.drop_table('audit_logs')