#!/usr/bin/env python3
"""
Update all cache-busting version numbers to latest version
"""

import re
from pathlib import Path

def update_cache_versions(base_dir, new_version):
    """Update all cache version parameters in HTML files"""
    updated_count = 0
    
    for html_file in Path(base_dir).rglob('*.htm*'):
        try:
            with open(html_file, 'r', encoding='utf-8', errors='ignore') as f:
                content = f.read()
            
            # Skip if already has latest version
            if f'?v={new_version}' in content:
                continue
            
            # Replace all version parameters
            new_content = re.sub(
                r'\?v=\d+',
                f'?v={new_version}',
                content
            )
            
            if new_content != content:
                with open(html_file, 'w', encoding='utf-8', errors='ignore') as f:
                    f.write(new_content)
                updated_count += 1
                
                if updated_count % 100 == 0:
                    print(f"Updated {updated_count} files...")
        
        except Exception as e:
            print(f"Error processing {html_file}: {e}")
            continue
    
    return updated_count

def main():
    base_dir = Path('/home/pts/Documents/QBPOS_Help_Web/QB_Help_Web/POS_Help')
    new_version = '20260110080000'
    
    print(f"Updating cache versions to: v={new_version}")
    print("=" * 60)
    
    updated = update_cache_versions(base_dir, new_version)
    
    print("=" * 60)
    print(f"✅ Updated {updated} files to version v={new_version}")

if __name__ == '__main__':
    main()
