import glob
import re

files = glob.glob('resources/views/livewire/admin/**/*.blade.php', recursive=True)

for filepath in files:
    with open(filepath, 'r') as f:
        content = f.read()

    # Find all modal blocks
    # A modal block starts with @if($var) and ends with the matching @endif
    
    # We will find the start of a modal:
    matches = list(re.finditer(r'@if\(\$([a-zA-Z0-9_]+)\)\s*(<div class="fixed inset-0 z-50 flex justify-end">)', content))
    
    if not matches:
        continue
        
    # Process from right to left so indices don't shift
    for match in reversed(matches):
        start_idx = match.start()
        var_name = match.group(1)
        
        # Find the matching @endif
        # We can just search for @if and @endif from start_idx to find the matching one
        depth = 0
        end_idx = -1
        
        # Tokenize by @if and @endif
        pos = start_idx
        while pos < len(content):
            next_if = content.find('@if', pos + 3)
            next_endif = content.find('@endif', pos + 3)
            
            if next_endif == -1:
                break
                
            if next_if != -1 and next_if < next_endif:
                depth += 1
                pos = next_if
            else:
                depth -= 1
                pos = next_endif
                if depth == 0:
                    end_idx = pos
                    break
        
        if end_idx != -1:
            # We found the block!
            # Let's extract it
            block = content[start_idx:end_idx + 6] # +6 for @endif
            
            # Now modify the block
            # 1. Remove @if($var)
            new_block = re.sub(r'^@if\(\$[a-zA-Z0-9_]+\)\s*', '', block)
            # 2. Remove @endif
            new_block = re.sub(r'\s*@endif$', '', new_block)
            
            # 3. Replace outer div
            new_block = new_block.replace(
                '<div class="fixed inset-0 z-50 flex justify-end">',
                f'<div x-data="{{ show: @entangle(\'{var_name}\') }}" x-show="show" x-cloak class="fixed inset-0 z-50 flex justify-end">'
            )
            
            # 4. Replace backdrop
            backdrop_pattern = r'<div class="absolute inset-0([^"]*)" wire:click="[^"]*"></div>'
            new_block = re.sub(backdrop_pattern, r'<div x-show="show" x-transition.opacity.duration.300ms class="absolute inset-0\1" @click="show = false"></div>', new_block)
            
            # 5. Replace panel
            panel_pattern = r'<div class="relative w-full md:w-3/4 lg:w-3/4 bg-white dark:bg-slate-900 shadow-2xl flex flex-col h-full animate-slide-in-right overflow-hidden border-l border-slate-200 dark:border-slate-800">'
            alpine_panel = r'''<div x-show="show"
             x-transition:enter="transition ease-out duration-300 transform"
             x-transition:enter-start="translate-x-full"
             x-transition:enter-end="translate-x-0"
             x-transition:leave="transition ease-in duration-200 transform"
             x-transition:leave-start="translate-x-0"
             x-transition:leave-end="translate-x-full"
             class="relative w-full md:w-3/4 lg:w-3/4 bg-white dark:bg-slate-900 shadow-2xl flex flex-col h-full overflow-hidden border-l border-slate-200 dark:border-slate-800">'''
            new_block = new_block.replace(panel_pattern, alpine_panel)
            
            # Replace in content
            content = content[:start_idx] + new_block + content[end_idx+6:]

    with open(filepath, 'w') as f:
        f.write(content)

print("Done")
