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 @if($variable) ... @endif blocks for modals
    # Modals usually start with @if($variable) \n <div class="fixed inset-0 ...
    
    # Let's find all occurrences of @if($var) ... <div class="fixed inset-0 z-50 flex justify-end">
    # We will replace it with Alpine x-data
    
    def replacer(match):
        var_name = match.group(1)
        # We need to replace the @if with nothing, and the outer div with x-data
        return f'<div x-data="{{ show: @entangle(\'{var_name}\') }}" x-show="show" x-cloak class="fixed inset-0 z-50 flex justify-end">'
        
    pattern = r'@if\(\$([a-zA-Z0-9_]+)\)\s*<div class="fixed inset-0 z-50 flex justify-end">'
    content = re.sub(pattern, replacer, content)
    
    # Replace the backdrop
    # <div class="absolute inset-0 bg-slate-900/40 dark:bg-slate-950/80 backdrop-blur-sm" wire:click="..."></div>
    # Actually some use bg-slate-900/50 etc.
    backdrop_pattern = r'<div class="absolute inset-0([^"]*)" wire:click="[^"]*"></div>'
    content = re.sub(backdrop_pattern, r'<div x-show="show" x-transition.opacity.duration.300ms class="absolute inset-0\1" @click="show = false"></div>', content)
    
    # Replace the inner panel
    # <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">
    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">'''
    content = content.replace(panel_pattern, alpine_panel)
    
    # Now we need to remove the @endif that corresponds to the @if
    # But this is tricky with regex. Instead of regex, maybe we can just find the end of the div?
    # Actually, the quickest way to remove @endif at the end of the modal is to find:
    # </div>\s*@endif  and replace with </div>
    # But let's be careful. Let's just do a string replacement if we know it's there.
    # Since we replaced the @if, there's an orphan @endif.
    
    # In each file, let's find the closing tags of the modals.
    content = re.sub(r'</div>\s*@endif', r'</div>', content)
    
    with open(filepath, 'w') as f:
        f.write(content)

print("Done")
