import os

def update_php_includes_in_dir(root_directory):
    search_footer = "b-footer.php"
    replace_footer = "z-footer.php"
    
    search_header = "b-header.php"
    replace_header = "z-header.php"
    
    updated_files_count = 0
    total_replacements = 0
    
    print(f"Scanning directory: {os.path.abspath(root_directory)}\n")
    
    for dirpath, _, filenames in os.walk(root_directory):
        for filename in filenames:
            if filename.endswith('.php'):
                file_path = os.path.join(dirpath, filename)
                
                try:
                    with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                        content = f.read()
                    
                    file_modified = False
                    replacements_in_file = 0
                    
                    # Replace footer
                    if search_footer in content:
                        content = content.replace(search_footer, replace_footer)
                        count_f = content.count(replace_footer) # Approximate count or track diffs
                        replacements_in_file += 1
                        file_modified = True
                        
                    # Replace header
                    if search_header in content:
                        content = content.replace(search_header, replace_header)
                        replacements_in_file += 1
                        file_modified = True
                        
                    if file_modified:
                        with open(file_path, 'w', encoding='utf-8') as f:
                            f.write(content)
                        
                        updated_files_count += 1
                        total_replacements += replacements_in_file
                        print(f"[UPDATED] {file_path} ({replacements_in_file} change(s))")
                        
                except Exception as e:
                    print(f"[ERROR] Could not process {file_path}: {e}")
                    
    print("\n--- Summary ---")
    print(f"Total files updated: {updated_files_count}")
    print(f"Total path replacements: {total_replacements}")

if __name__ == "__main__":
    # Point this to your web project root folder
    target_dir = "." 
    update_php_includes_in_dir(target_dir)