import re

with open('app/routes.py', 'r', encoding='utf-8') as f:
    text = f.read()

old_str = """    print_file = PrintFile(
        filename=unique_filename,
        original_filename=f"{combined_name}.stl",
        file_type='stl',
        user_id=current_user.id,
        status='waiting'
    )
    db.session.add(print_file)
    db.session.commit()
    
    slice_stl_task(print_file.id, merged_filepath, quality)"""

new_str = """    print_file = PrintFile(
        filename=unique_filename,
        original_filename=f"{combined_name}.stl",
        file_type='stl',
        user_id=current_user.id,
        status='merging'
    )
    db.session.add(print_file)
    db.session.commit()
    
    from .tasks import merge_and_slice_task
    merge_and_slice_task(print_file.id, inputs, merged_filepath, quality)"""

if old_str in text:
    with open('app/routes.py', 'w', encoding='utf-8') as f:
        f.write(text.replace(old_str, new_str))
    print("Patched successfully")
else:
    print("Old string not found")
