#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Update SCENES in __init__.py to Fases 56-61"""

import re

file_path = r'C:\ProgramData\Ableton\Live 12 Suite\Resources\MIDI Remote Scripts\AbletonMCP_AI\__init__.py'

with open(file_path, 'r', encoding='utf-8') as f:
    content = f.read()

# The old SCENES definition (what we're replacing)
old_scenes_start = '# ================================================================'
old_scenes_marker1 = '# SCENE DEFINITIONS'
old_scenes_marker2 = 'SCENES = ['

# Find the start of SCENES section
start_idx = content.find('# SCENE DEFINITIONS (12 scenes for Fases 16-20)')
if start_idx == -1:
    start_idx = content.find('# SCENE DEFINITIONS (Fases 56-61: Scenes 0-5)')
    if start_idx != -1:
        print('INFO: File already has Fases 56-61')
        exit(0)

if start_idx == -1:
    print('ERROR: Could not find SCENE DEFINITIONS section')
    # Try to find any SCENE DEFINITIONS
    idx = content.find('# SCENE DEFINITIONS')
    if idx != -1:
        print(f'Found SCENE DEFINITIONS at position {idx}')
        print('Context:', content[idx:idx+100])
    exit(1)

# Find the end of SCENES list (FX_BY_SCENE closing brace)
end_marker = '# FASE 19: NO_REPEAT'
end_idx = content.find(end_marker, start_idx)
if end_idx == -1:
    end_idx = content.find('# FASE 20: Energy-based', start_idx)
    
if end_idx == -1:
    print('ERROR: Could not find end of SCENES section')
    exit(1)

# Extract the section to replace
old_section = content[start_idx:end_idx]

print(f'Found section from {start_idx} to {end_idx} ({len(old_section)} chars)')

# New SCENES definition
new_section = '''# ================================================================
        # SCENE DEFINITIONS (Fases 56-61: Scenes 0-5)
        # ================================================================
        SCENES = [
            # Fase 56: Scene 0 - Intro (NO drums)
            ("Intro", 4, 0.20, {
                "drums": False, "bass": False, "lead": False,
                "chords": "intro", "pad": True, "ambience": True, "hat": False,
                "riser": False, "impact": False
            }),
            # Fase 57: Scene 1 - Verse A (sparse drums, intensity 0.6)
            ("Verse A", 8, 0.50, {
                "drums": True, "bass": True, "lead": False,
                "chords": "verse_standard", "pad": False, "ambience": False, "hat": True,
                "drum_intensity": 0.6, "bass_style": "sub"
            }),
            # Fase 58: Scene 2 - Verse B (agrega lead melody)
            ("Verse B", 8, 0.60, {
                "drums": True, "bass": True, "lead": True,
                "chords": "verse_alt1", "pad": False, "ambience": False, "hat": True,
                "drum_intensity": 0.7, "bass_style": "standard"
            }),
            # Fase 59: Scene 3 - Pre-Chorus (riser y anticipation)
            ("Pre-Chorus", 4, 0.75, {
                "drums": True, "bass": True, "lead": False,
                "chords": "prechorus", "pad": True, "ambience": False, "hat": True,
                "riser": True, "drum_intensity": 0.8, "anticipation": True
            }),
            # Fase 60: Scene 4 - Chorus A (impact y maxima energia)
            ("Chorus A", 8, 0.95, {
                "drums": True, "bass": True, "lead": True,
                "chords": "chorus_power", "pad": True, "ambience": False, "hat": True,
                "impact": True, "drum_intensity": 1.0, "bass_style": "melodic"
            }),
            # Fase 61: Scene 5 - Chorus B (modulacion +1 semitono)
            ("Chorus B", 8, 0.90, {
                "drums": True, "bass": True, "lead": True,
                "chords": "chorus_alternative", "pad": False, "ambience": False, "hat": True,
                "drum_intensity": 0.95, "bass_style": "octaves", "modulation": "+1"
            }),
        ]

        # Scene indices with drums active (for Perc Loops, etc.)
        PERC_LOOP_SCENES = [1, 2, 3, 4, 5]  # All except Intro (0)
        DRUMLOOP_SCENES = [1, 2, 3, 4, 5]   # All except Intro (0)
        PROTAGONIST_SCENES = [2, 4]         # Main scenes for protagonist drumloop

        # FX assignments by scene (extended params)
        FX_BY_SCENE = {
            3: "riser",      # Pre-Chorus: Riser
            4: "impact",     # Chorus A: Impact
        }

        '''

# Replace
new_content = content[:start_idx] + new_section + content[end_idx:]

with open(file_path, 'w', encoding='utf-8') as f:
    f.write(new_content)

print('SUCCESS: SCENES updated to Fases 56-61')
print('Scenes 0-5 configured: Intro, Verse A, Verse B, Pre-Chorus, Chorus A, Chorus B')
