import os
import re

directory = '/Applications/XAMPP/xamppfiles/htdocs/aaa'
count = 0

for root, _, files in os.walk(directory):
    for filename in files:
        if filename.endswith('.php'):
            filepath = os.path.join(root, filename)
            
            with open(filepath, 'r', encoding='utf-8') as f:
                try:
                    content = f.read()
                except UnicodeDecodeError:
                    continue
                
            orig = content
            
            # Universal responsive replacements for the internal pages!
            
            # 1. <div class="grid grid-cols-12 gap-5"> => Replace with <div class="row g-4">
            content = re.sub(r'class="([^"]*)grid\s+grid-cols-12(?:\s+gap-5)?([^"]*)"', r'class="\1row g-4\2"', content)
            
            # 2. class=" pt-spacing20 grid gap-spacing20 grid-cols-12"
            content = re.sub(r'class="([^"]*)grid\s+gap-spacing20\s+grid-cols-12([^"]*)"', r'class="\1row g-4\2"', content)
            
            # 3. class="grid-cols-9  grid  gap-5 items-center "
            content = re.sub(r'class="([^"]*)grid-cols-9\s+grid\s+gap-5([^"]*)"', r'class="\1row g-4 align-items-center\2"', content)

            def replace_cols(match):
                classes = match.group(0)
                
                # Map various md:col-span to bootstrap
                classes = classes.replace('md:col-span-7', 'col-12 col-md-7')
                classes = classes.replace('md:col-span-5', 'col-12 col-md-5')
                classes = classes.replace('md:col-span-8', 'col-12 col-md-8')
                classes = classes.replace('md:col-span-3', 'col-12 col-md-3')
                classes = classes.replace('md:col-span-9', 'col-12 col-md-9')
                classes = classes.replace('md:col-span-12', 'col-12')
                classes = classes.replace('col-span-4', 'col-12 col-md-5') # mapping 4/9 to 5/12 mostly aligns layout
                classes = classes.replace('col-span-12', 'col-12')
                
                # Cleanup duplicates
                parts = classes.split('"')
                if len(parts) >= 2:
                    cls_list = parts[1].split()
                    new_list = []
                    for c in cls_list:
                        # exclude w-full if we have col-12 as it's redundant/sometimes conflict
                        if c == 'w-full' and 'col-12' in new_list:
                            continue
                        if c not in new_list:
                            new_list.append(c)
                    classes = parts[0] + '"' + ' '.join(new_list) + '"' + (parts[2] if len(parts)>2 else "")
                return classes

            # Only do substitutions for class attributes that have col-span in them
            content = re.sub(r'class="[^"]*(col-span-|md:col-span-)[^"]*"', replace_cols, content)
            
            if content != orig:
                with open(filepath, 'w', encoding='utf-8') as f:
                    f.write(content)
                count += 1

print(f"Updated {count} files.")
