#!/usr/bin/env python3
"""
Fix Unicode encoding issues in the application
"""

import os
import re

def fix_unicode_in_file(filepath):
    """Fix Unicode characters in a single file"""
    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Common Unicode replacements
        replacements = {
            'SUCCESS:': 'SUCCESS:',
            'ERROR:': 'ERROR:',
            'WARNING:': 'WARNING:',
            'SUCCESS:': 'SUCCESS:',
            ' to ': ' to ',
            'UP': 'UP',
            'DOWN': 'DOWN',
            '*': '*',
            '-': '-',
        }
        
        original_content = content
        for unicode_char, replacement in replacements.items():
            content = content.replace(unicode_char, replacement)
        
        if content != original_content:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"Fixed Unicode in: {filepath}")
            return True
        
        return False
        
    except Exception as e:
        print(f"Error processing {filepath}: {e}")
        return False

def main():
    """Main execution"""
    print("Fixing Unicode issues in Python files...")
    
    # Directories to check
    directories = ['loans', 'utils', '.']
    
    fixed_files = []
    
    for directory in directories:
        if os.path.exists(directory):
            for root, dirs, files in os.walk(directory):
                for file in files:
                    if file.endswith('.py'):
                        filepath = os.path.join(root, file)
                        if fix_unicode_in_file(filepath):
                            fixed_files.append(filepath)
    
    if fixed_files:
        print(f"\nFixed Unicode issues in {len(fixed_files)} files:")
        for filepath in fixed_files:
            print(f"  - {filepath}")
    else:
        print("\nNo Unicode issues found or all already fixed.")
    
    print("\nUnicode fix completed!")

if __name__ == "__main__":
    main()