import sys
import os

path = r'C:\ProgramData\Ableton\Live 12 Suite\Resources\MIDI Remote Scripts\AbletonMCP_AI\abletonmcp_runtime.py'
with open(path, 'r', encoding='utf-8') as f:
    lines = f.readlines()

new_lines = []
for i in range(len(lines)):
    if 'created_clip = track.create_audio_clip(resolved_path, float(position))' in lines[i]:
        new_lines.append('                success = False\n')
        new_lines.append('                created_clip = None\n')
        new_lines.append('                for attempt in range(3):\n')
        new_lines.append('                    try:\n')
        new_lines.append('                        created_clip = track.create_audio_clip(resolved_path, float(position))\n')
        new_lines.append('                    except Exception as e:\n')
        new_lines.append('                        self.log_message("Warning: Clip creation error at attempt " + str(attempt+1) + ": " + str(e))\n')
        new_lines.append('                    import time\n')
        new_lines.append('                    time.sleep(0.1)\n')
        new_lines.append('                    clip_persisted = False\n')
        new_lines.append('                    for clip in getattr(track, "arrangement_clips", getattr(track, "clips", [])):\n')
        new_lines.append('                        if hasattr(clip, "start_time") and abs(float(clip.start_time) - float(position)) < 0.05:\n')
        new_lines.append('                            clip_persisted = True\n')
        new_lines.append('                            created_clip = clip\n')
        new_lines.append('                            break\n')
        new_lines.append('                    if clip_persisted:\n')
        new_lines.append('                        success = True\n')
        new_lines.append('                        break\n')
        new_lines.append('                    self.log_message("Warning: Clip at " + str(position) + " not persisted on attempt " + str(attempt+1))\n')
        new_lines.append('                    time.sleep(0.1)\n')
        new_lines.append('                if not success:\n')
        new_lines.append('                    self.log_message("Error: Failed to persist clip at " + str(position) + " after 3 attempts")\n')
        new_lines.append('                    continue\n')
        continue
    
    if 'else:' in lines[i] and i+1 < len(lines) and 'for clip in getattr(track, "clips", []):' in lines[i+1]:
        new_lines.append('                        pass # ' + lines[i].lstrip())
        continue
    if i > 0 and 'else:' in lines[i-1] and 'for clip in getattr(track, "clips", []):' in lines[i]:
        new_lines.append('                        # ' + lines[i].lstrip())
        continue
    if i > 1 and 'else:' in lines[i-2] and 'if hasattr(clip, "start_time")' in lines[i]:
        new_lines.append('                        # ' + lines[i].lstrip())
        continue
    if i > 2 and 'else:' in lines[i-3] and 'if hasattr(clip, "name"):' in lines[i]:
        new_lines.append('                        # ' + lines[i].lstrip())
        continue
    if i > 3 and 'else:' in lines[i-4] and 'clip.name = clip_name' in lines[i]:
        new_lines.append('                        # ' + lines[i].lstrip())
        continue
    if i > 4 and 'else:' in lines[i-5] and 'break' in lines[i]:
        new_lines.append('                        # ' + lines[i].lstrip())
        continue

    new_lines.append(lines[i])

with open(path, 'w', encoding='utf-8') as f:
    f.writelines(new_lines)
print('Done patching')
