import os
import re

# Script to find the problematic line in your Python files

def find_bad_line(directory=".", file_pattern="*.py"):
    """Search for the problematic categorizer.get_detailed_category(developers_id) line"""
    
    print("Searching for the problematic line...")
    print("-" * 50)
    
    found = False
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.py'):
                filepath = os.path.join(root, file)
                try:
                    with open(filepath, 'r', encoding='utf-8') as f:
                        lines = f.readlines()
                        
                    for i, line in enumerate(lines, 1):
                        # Look for the problematic pattern
                        if 'get_detailed_category' in line and ('developers_id' in line or 'developer_id' in line):
                            print(f"\n❌ FOUND PROBLEM in {filepath}")
                            print(f"   Line {i}: {line.strip()}")
                            print(f"   This line is causing your app to crash!")
                            print(f"   DELETE this line or comment it out with #")
                            found = True
                            
                        # Also check for other potential issues
                        if 'get_detailed_category(' in line and line.strip().endswith('_id)'):
                            print(f"\n⚠️  Suspicious line in {filepath}")
                            print(f"   Line {i}: {line.strip()}")
                            print(f"   This might be the problem")
                            found = True
                            
                except Exception as e:
                    pass
    
    if not found:
        print("\n✅ No problematic lines found in Python files")
        print("\nThe error might be in a different format. Check your activity_categorization_api.py manually")
        print("Look for any call to get_detailed_category that doesn't have two parameters:")
        print("  CORRECT: get_detailed_category(window_title, application_name)")
        print("  WRONG: get_detailed_category(some_id)")
    else:
        print("\n" + "-" * 50)
        print("Fix the issues above, save the file, then run:")
        print("pm2 restart timesheet-backend")

if __name__ == "__main__":
    # Search in current directory
    find_bad_line(".")
    
    # Also specifically check the timesheet directory if it exists
    if os.path.exists("E:/timesheet/timesheet_new"):
        print("\n\nChecking timesheet directory specifically...")
        find_bad_line("E:/timesheet/timesheet_new")
