"""
Replace all Font Awesome icons with emojis in expense templates
"""

import re
from pathlib import Path

# Icon mapping: Font Awesome class -> Emoji
ICON_MAP = {
    'fa-arrow-left': '←',
    'fa-plus': '+',
    'fa-check-circle': '✓',
    'fa-check': '✓',
    'fa-file-excel': '📊',
    'fa-chart-bar': '📊',
    'fa-filter': '🔍',
    'fa-times': '✕',
    'fa-edit': '✏️',
    'fa-trash': '🗑️',
    'fa-clock': '⏰',
    'fa-times-circle': '✕',
    'fa-file-alt': '📄',
    'fa-save': '💾',
    'fa-exclamation-circle': '⚠️',
    'fa-exclamation-triangle': '⚠️',
    'fa-link': '🔗',
    'fa-minus': '−',
    'fa-inbox': '📥',
    'fa-chevron-left': '‹',
    'fa-chevron-right': '›',
}

def replace_icons_in_file(filepath):
    """Replace Font Awesome icons with emojis in a single file"""
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    original_content = content
    replacements = 0
    
    # Pattern to match Font Awesome icons
    # Matches: <i class="fas fa-ICON ..."></i>
    pattern = r'<i class="fas (fa-[\w-]+)[^"]*"[^>]*></i>'
    
    def replace_icon(match):
        nonlocal replacements
        fa_class = match.group(1)
        if fa_class in ICON_MAP:
            replacements += 1
            emoji = ICON_MAP[fa_class]
            return f'<span>{emoji}</span>'
        return match.group(0)  # Return original if no mapping
    
    content = re.sub(pattern, replace_icon, content)
    
    if content != original_content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"✓ {filepath.name}: {replacements} icons replaced")
        return replacements
    else:
        print(f"  {filepath.name}: No changes needed")
        return 0

def main():
    print("=" * 60)
    print("REPLACING FONT AWESOME ICONS WITH EMOJIS")
    print("=" * 60)
    print()
    
    template_dir = Path('templates/expenses')
    total_replacements = 0
    
    if not template_dir.exists():
        print(f"Error: Directory {template_dir} not found")
        return
    
    html_files = list(template_dir.glob('*.html'))
    
    if not html_files:
        print(f"No HTML files found in {template_dir}")
        return
    
    print(f"Found {len(html_files)} template files\n")
    
    for filepath in sorted(html_files):
        count = replace_icons_in_file(filepath)
        total_replacements += count
    
    print()
    print("=" * 60)
    print(f"COMPLETE: {total_replacements} total icons replaced")
    print("=" * 60)
    print()
    print("Icon Mapping Used:")
    for fa, emoji in sorted(ICON_MAP.items()):
        print(f"  {fa:25} → {emoji}")

if __name__ == '__main__':
    main()
