#!/usr/bin/env python3
"""
Update all documentation files to use hkia naming convention.
"""

import re
from pathlib import Path
import logging

logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)

def update_doc_file(file_path: Path) -> bool:
    """Update documentation file to use hkia naming."""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        original_content = content
        
        # Replace various forms of the old naming
        patterns = [
            # Basic replacements
            (r'hvacknowitall', 'hkia'),
            (r'hvacnkowitall', 'hkia'),
            (r'HVACKNOWITALL', 'HKIA'),
            (r'HVACNKOWITALL', 'HKIA'),
            
            # Full name replacements
            (r'HVAC Know It All', 'HKIA'),
            (r'HVAC KNOW IT ALL', 'HKIA'),
            (r'HvacKnowItAll', 'HKIA'),
            
            # Path replacements
            (r'/mnt/nas/hvacknowitall', '/mnt/nas/hkia'),
            (r'hvac-kia-content', 'hvac-kia-content'),  # Keep repo name as is
            
            # Service name replacements
            (r'hvac-content-', 'hkia-content-'),
            (r'hvac-scraper', 'hkia-scraper'),
            
            # Keep these unchanged (repo/project names)
            (r'github\.com/bengizmo/hkia-content', 'github.com/bengizmo/hvacknowitall-content'),
            (r'hvac-kia-content', 'hvac-kia-content'),  # project directory
        ]
        
        for pattern, replacement in patterns:
            content = re.sub(pattern, replacement, content, flags=re.IGNORECASE if not pattern.startswith('/') else 0)
        
        # Fix any double replacements
        content = content.replace('hkia-kia', 'hvac-kia')
        
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            logger.info(f"✅ Updated: {file_path.name}")
            return True
        else:
            logger.info(f"ℹ️  No changes needed: {file_path.name}")
        return False
    except Exception as e:
        logger.error(f"❌ Error updating {file_path}: {e}")
        return False

def main():
    """Main update process for documentation."""
    logger.info("=" * 60)
    logger.info("UPDATING DOCUMENTATION TO HKIA NAMING")
    logger.info("=" * 60)
    
    base_dir = Path('/home/ben/dev/hvac-kia-content')
    
    # Documentation files to update
    doc_files = [
        'README.md',
        'CLAUDE.md',
        'DEPLOY.md',
        'docs/project_specification.md',
        'docs/image_downloads.md',
        'docs/status.md',
        'docs/final_status.md',
        'docs/deployment_strategy.md',
        'docs/DEPLOYMENT_CHECKLIST.md',
        'docs/PRODUCTION_TODO.md',
        'BACKLOG_STATUS.md',
        'UPDATED_CAPTURE_STATUS.md',
        'FINAL_TALLY_REPORT.md',
    ]
    
    # Update documentation files
    logger.info("\n📝 Updating documentation files...")
    updated_count = 0
    
    for file_path in doc_files:
        full_path = base_dir / file_path
        if full_path.exists():
            if update_doc_file(full_path):
                updated_count += 1
        else:
            logger.info(f"⚠️  File not found: {file_path}")
    
    # Summary
    logger.info("\n" + "=" * 60)
    logger.info("DOCUMENTATION UPDATE COMPLETE")
    logger.info("=" * 60)
    logger.info(f"Files updated: {updated_count}")
    logger.info("\nNext steps:")
    logger.info("1. Review changes with 'git diff'")
    logger.info("2. Commit documentation updates")
    logger.info("3. Push to repository")

if __name__ == "__main__":
    main()