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 the modal block
    matches = list(re.finditer(r'@if\(\$([a-zA-Z0-9_]+)\)\s*(<div class="fixed inset-0 z-50 flex justify-(?:start|end)">)', content))
    
    if not matches:
        continue
        
    for match in reversed(matches):
        start_idx = match.start()
        var_name = match.group(1)
        
        depth = 0
        end_idx = -1
        
        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:
            block = content[start_idx:end_idx + 6]
            
            # Remove @if and @endif
            new_block = re.sub(r'^@if\(\$[a-zA-Z0-9_]+\)\s*', '', block)
            new_block = re.sub(r'\s*@endif$', '', new_block)
            
            # Replace outer div
            new_block = re.sub(
                r'<div class="fixed inset-0 z-50 flex justify-(?:start|end)">',
                f'<div x-data="{{ show: @entangle(\'{var_name}\') }}" x-show="show" x-cloak class="fixed inset-0 z-50 flex justify-end">',
                new_block
            )
            
            # Replace backdrop
            new_block = re.sub(
                r'<div class="absolute inset-0([^"]*)" wire:click="[^"]*"></div>',
                r'<div x-show="show" x-transition.opacity.duration.300ms class="absolute inset-0\1" @click="show = false"></div>',
                new_block
            )
            
            # Replace panel
            # It could have animate-slide-in-left or animate-slide-in-right, border-l or border-r
            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-(?:left|right) overflow-hidden border-(?:l|r) 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 = re.sub(panel_pattern, alpine_panel, new_block)
            
            content = content[:start_idx] + new_block + content[end_idx+6:]

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

print("Done")
