#!/usr/bin/env python3
"""
Update all references from hvacknowitall/hvacnkowitall to hkia in codebase and rename files.
"""

import os
import re
import shutil
from pathlib import Path
import logging

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

def update_file_content(file_path: Path) -> bool:
    """Update content in a 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 = [
            (r'hvacknowitall', 'hkia'),
            (r'hvacnkowitall', 'hkia'),
            (r'HVACKNOWITALL', 'HKIA'),
            (r'HVACNKOWITALL', 'HKIA'),
            (r'HvacKnowItAll', 'HKIA'),
            (r'HVAC Know It All', 'HKIA'),
            (r'HVAC KNOW IT ALL', 'HKIA'),
        ]
        
        for pattern, replacement in patterns:
            content = re.sub(pattern, replacement, content)
        
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            logger.info(f"✅ Updated: {file_path}")
            return True
        return False
    except Exception as e:
        logger.error(f"❌ Error updating {file_path}: {e}")
        return False

def rename_markdown_files(directory: Path) -> list:
    """Rename markdown files to use hkia naming."""
    renamed_files = []
    
    for md_file in directory.rglob('*.md'):
        old_name = md_file.name
        new_name = old_name
        
        # Replace various patterns
        if 'hvacknowitall' in old_name:
            new_name = old_name.replace('hvacknowitall', 'hkia')
        elif 'hvacnkowitall' in old_name:
            new_name = old_name.replace('hvacnkowitall', 'hkia')
        
        if new_name != old_name:
            new_path = md_file.parent / new_name
            try:
                md_file.rename(new_path)
                logger.info(f"📝 Renamed: {old_name} → {new_name}")
                renamed_files.append((str(md_file), str(new_path)))
            except Exception as e:
                logger.error(f"❌ Error renaming {md_file}: {e}")
    
    return renamed_files

def main():
    """Main update process."""
    logger.info("=" * 60)
    logger.info("UPDATING TO HKIA NAMING CONVENTION")
    logger.info("=" * 60)
    
    base_dir = Path('/home/ben/dev/hvac-kia-content')
    
    # Files to update (excluding test files and git)
    files_to_update = [
        'src/base_scraper.py',
        'src/orchestrator.py',
        'src/instagram_scraper.py',
        'src/instagram_scraper_with_images.py',
        'src/instagram_scraper_cumulative.py',
        'src/youtube_scraper.py',
        'src/youtube_api_scraper.py',
        'src/youtube_api_scraper_with_thumbnails.py',
        'src/rss_scraper.py',
        'src/rss_scraper_with_images.py',
        'src/wordpress_scraper.py',
        'src/tiktok_scraper.py',
        'src/tiktok_scraper_advanced.py',
        'src/mailchimp_api_scraper_v2.py',
        'src/cumulative_markdown_manager.py',
        'run_production.py',
        'run_production_with_images.py',
        'run_production_cumulative.py',
        'run_instagram_next_1000.py',
        'production_backlog_capture.py',
        'README.md',
        'CLAUDE.md',
        'docs/project_specification.md',
        'docs/image_downloads.md',
        '.env.production',
        'deploy/hvac-content-8am.service',
        'deploy/hvac-content-12pm.service',
        'deploy/hvac-content-images-8am.service',
        'deploy/hvac-content-images-12pm.service',
        'deploy/hvac-content-cumulative-8am.service',
        'deploy/update_to_images.sh',
        'deploy_production.sh',
    ]
    
    # Update file contents
    logger.info("\n📝 Updating file contents...")
    updated_count = 0
    for file_path in files_to_update:
        full_path = base_dir / file_path
        if full_path.exists():
            if update_file_content(full_path):
                updated_count += 1
    
    logger.info(f"\n✅ Updated {updated_count} files with new naming convention")
    
    # Rename markdown files
    logger.info("\n📁 Renaming markdown files...")
    
    # Directories to check for markdown files
    markdown_dirs = [
        base_dir / 'data' / 'markdown_current',
        base_dir / 'data' / 'markdown_archives',
        base_dir / 'data_production_backlog' / 'markdown_current',
        base_dir / 'test_data',
    ]
    
    all_renamed = []
    for directory in markdown_dirs:
        if directory.exists():
            logger.info(f"\nChecking {directory}...")
            renamed = rename_markdown_files(directory)
            all_renamed.extend(renamed)
    
    logger.info(f"\n✅ Renamed {len(all_renamed)} markdown files")
    
    # Summary
    logger.info("\n" + "=" * 60)
    logger.info("UPDATE COMPLETE")
    logger.info("=" * 60)
    logger.info(f"Files updated: {updated_count}")
    logger.info(f"Files renamed: {len(all_renamed)}")
    logger.info("\nNext steps:")
    logger.info("1. Review changes with 'git diff'")
    logger.info("2. Test scrapers to ensure they work with new naming")
    logger.info("3. Commit changes")
    logger.info("4. Run rsync to update NAS with new naming")

if __name__ == "__main__":
    main()