<?php
/**
 * UI Auditor v2 - Advanced Tailwind Fixer
 */
$dir = new RecursiveDirectoryIterator('c:/xampp/htdocs/inveetaire/app/views');
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

$modifiedFiles = [];

foreach ($files as $file) {
    $path = $file[0];
    if (strpos($path, 'layouts') !== false || strpos($path, 'public') !== false) {
        continue; // skip
    }
    
    $originalContent = file_get_contents($path);
    $content = $originalContent;

    // 1. Fix flex gaps (any flex container containing a button or anchor that looks like a button without gap)
    // We'll replace class="flex " with class="flex gap-3 " if it doesn't already have gap.
    // However, it's safer to just find class="... flex ..." that precedes <button or <a class="btn
    // Regex is hard for this. Let's do it simply:
    // Look for `<div class="flex ">` or similar and if there's no gap, add gap-3.
    // Wait, flex is used for many things.
    // Let's target flex containers that have `btn` children.
    
    // Instead of regex for full parsing, let's target the exact problem the user mentioned:
    // percentage widths in flex containers with gaps causing buttons to touch.
    // Match `w-1/3`, `w-2/3`, `w-1/2`, `w-1/4`, `w-3/4` on elements that are inside a flex container or just in general if they are buttons.
    $content = preg_replace_callback('/<a([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-1\/2\b([^"]*)"([^>]*)>/i', function($m) {
        return '<a' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[1]' . $m[4] . '"' . $m[5] . '>';
    }, $content);
    $content = preg_replace_callback('/<button([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-1\/2\b([^"]*)"([^>]*)>/i', function($m) {
        return '<button' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[1]' . $m[4] . '"' . $m[5] . '>';
    }, $content);

    // 1/3 and 2/3
    $content = preg_replace_callback('/<a([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-1\/3\b([^"]*)"([^>]*)>/i', function($m) {
        return '<a' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[1]' . $m[4] . '"' . $m[5] . '>';
    }, $content);
    $content = preg_replace_callback('/<button([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-1\/3\b([^"]*)"([^>]*)>/i', function($m) {
        return '<button' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[1]' . $m[4] . '"' . $m[5] . '>';
    }, $content);
    
    $content = preg_replace_callback('/<a([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-2\/3\b([^"]*)"([^>]*)>/i', function($m) {
        return '<a' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[2]' . $m[4] . '"' . $m[5] . '>';
    }, $content);
    $content = preg_replace_callback('/<button([^>]*)class="([^"]*)\bbtn\b([^"]*)\bw-2\/3\b([^"]*)"([^>]*)>/i', function($m) {
        return '<button' . $m[1] . 'class="' . $m[2] . 'btn' . $m[3] . 'flex-[2]' . $m[4] . '"' . $m[5] . '>';
    }, $content);

    // 2. Add gap-3 to any flex container that holds buttons but lacks gap
    // We can do a string search: find `<div class="[^"]*\bflex\b(?!.*\bgap-)[^"]*">\s*<button`
    $content = preg_replace('/class="([^"]*\bflex\b(?!.*\bgap-[0-9])[^"]*)"(>\s*<(?:button|a)[^>]*class="[^"]*\bbtn\b)/i', 'class="$1 gap-3"$3', $content);
    $content = preg_replace('/class="([^"]*\bflex\b(?!.*\bgap-[0-9])[^"]*)"(>\s*<form[^>]*>\s*<(?:button|a)[^>]*class="[^"]*\bbtn\b)/i', 'class="$1 gap-3"$3', $content);

    // 3. Same for modals: `<form class="flex">` -> `<form class="flex gap-3">`
    $content = preg_replace('/<form([^>]*)class="([^"]*\bflex\b(?!.*\bgap-[0-9])[^"]*)"([^>]*)>/i', '<form$1class="$2 gap-3"$4>', $content);


    // 4. Form inputs consistency. Replace py-2 px-3 with py-2.5 px-3.5 or standardize.
    // "form-input" should have standard height. Usually `text-xs` or `text-sm`.
    // Let's just make sure padding isn't weird.
    
    if ($content !== $originalContent) {
        file_put_contents($path, $content);
        $modifiedFiles[] = str_replace('c:/xampp/htdocs/inveetaire/app/views/', '', str_replace('\\', '/', $path));
    }
}

echo "Modified files:\n";
print_r($modifiedFiles);
