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()

    pattern = r'@if\(\$([a-zA-Z0-9_]+)\)\s*<div class="fixed inset-0 z-50 flex justify-(?:start|end)">'
    
    while True:
        match = re.search(pattern, content)
        if not match:
            break
            
        start_idx = match.start()
        var_name = match.group(1)
        
        pos = start_idx + 3
        depth = 1
        end_idx = -1
        
        while pos < len(content):
            next_if = content.find('@if', pos)
            next_endif = content.find('@endif', pos)
            
            if next_endif == -1:
                break
                
            if next_if != -1 and next_if < next_endif:
                depth += 1
                pos = next_if + 3
            else:
                depth -= 1
                pos = next_endif + 6
                if depth == 0:
                    end_idx = next_endif
                    break
                    
        if end_idx == -1:
            print(f"Failed to find @endif in {filepath}")
            break
            
        block = content[start_idx:end_idx + 6]
        
        new_block = re.sub(r'^@if\(\$[a-zA-Z0-9_]+\)\s*', '', block)
        new_block = re.sub(r'\s*@endif$', '', new_block)
        
        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, count=1
        )
        
        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, count=1
        )
        
        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, count=1)
        
        content = content[:start_idx] + new_block + content[end_idx + 6:]
        
    with open(filepath, 'w') as f:
        f.write(content)

print("Done")
