import os
import re

def camel_to_hyphen(name):
    # Remove 'presszoneComments' prefix
    if name.startswith('presszoneComments'):
        name = name[len('presszoneComments'):]
    elif name.startswith('PresszoneComments'):
        name = name[len('PresszoneComments'):]
    
    # Convert remaining CamelCase to hyphenated lowercase
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
    res = re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
    
    return 'presszone-comments-' + res.lstrip('-')

def refactor_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read()
    except Exception as e:
        print(f"Could not read {file_path}: {e}")
        return

    # 1. Standardize hyphenated prefix
    content = content.replace('presszone-comments-', 'presszone-comments-')

    # 2. Fix JS variables that were incorrectly hyphenated
    content = content.replace('window.presszone-comments-data', 'window.presszoneCommentsData')
    content = content.replace('window.presszone-comments-config', 'window.presszoneCommentsConfig')
    content = content.replace('window.presszone-comments-admin', 'window.presszoneCommentsAdmin')
    
    # Standardize camelCase
    content = content.replace('presszoneComments', 'presszoneComments')
    
    # Also handle some missed ones from previous runs
    content = content.replace('presszone-comments-data', 'presszoneCommentsData')
    content = content.replace('presszone-comments-config', 'presszoneCommentsConfig')
    content = content.replace('presszone-comments-admin', 'presszoneCommentsAdmin')
    
    # Wait, I might have messed up handles. Handles SHOULD be hyphenated.
    # Script handles like 'presszone-comments-frontend' are fine.
    # It's only the localized OBJECT NAME that should be camelCase.
    
    # Let's manually fix the most important files if needed, but a script is better.
    
    # Re-fix the specific JS variable names
    content = content.replace('window.presszoneCommentsData', 'window.presszoneCommentsData') # No change
    
    # 4. Handle keyframes ( CamelCase to hyphenated )
    def replace_keyframes(match):
        return camel_to_hyphen(match.group(0))

    if file_path.endswith(('.scss', '.css')):
        content = re.sub(r'presszoneComments[a-zA-Z0-9]+', replace_keyframes, content)

    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
    except Exception as e:
        print(f"Could not write {file_path}: {e}")

def main():
    dirs_to_scan = ['assets/css', 'assets/js', 'templates', 'admin/src-vanilla', 'includes']
    extensions = ('.scss', '.css', '.js', '.php')
    
    for d in dirs_to_scan:
        if not os.path.exists(d):
            continue
        for root, dirs, files in os.walk(d):
            for file in files:
                if file.endswith(extensions):
                    file_path = os.path.join(root, file)
                    print(f"Refactoring {file_path}...")
                    refactor_file(file_path)
    
    if os.path.exists('comments-press-zone.php'):
        print("Refactoring comments-press-zone.php...")
        refactor_file('comments-press-zone.php')

if __name__ == "__main__":
    main()