# Quick Fix for activity_categorization_api.py

Add this function to your existing `activity_categorization_api.py` file (don't replace the whole file, just add this function and update the relevant section):

```python
def extract_project_name_from_window(window_title, app_name):
    """Extract project name based on the patterns in your data"""
    if not window_title:
        return None
        
    window_title = window_title.strip()
    app_name = (app_name or "").lower()
    
    # Cursor patterns
    if app_name == 'cursor.exe' or 'cursor' in app_name:
        if ' - Cursor' in window_title:
            # Remove " - Cursor" from the end
            base = window_title.replace(' - Cursor', '')
            # If there's another dash, get the last part (project name)
            if ' - ' in base:
                parts = base.split(' - ')
                return parts[-1].strip()
            else:
                # Just project name
                return base.strip()
    
    # VS Code patterns  
    elif app_name in ['code.exe', 'code'] or 'visual studio code' in app_name:
        if ' - Visual Studio Code' in window_title:
            # Remove " - Visual Studio Code" from the end
            base = window_title.replace(' - Visual Studio Code', '')
            # If there's another dash, get the last part (project name)
            if ' - ' in base:
                parts = base.split(' - ')
                return parts[-1].strip()
            else:
                # Just project name
                return base.strip()
    
    # FileZilla pattern
    elif 'filezilla' in app_name:
        if ' - FileZilla' in window_title:
            return window_title.replace(' - FileZilla', '').strip()
    
    return None

# Then update the group_productive_activities_by_project function:
def group_productive_activities_by_project(activities):
    """Group productive activities by actual project name"""
    project_groups = {}
    
    for activity in activities:
        window_title = activity.get("window_title", "")
        app_name = activity.get("application_name", "")
        
        # Skip browser activities
        if app_name.lower() in ['chrome.exe', 'firefox.exe', 'edge.exe']:
            continue
            
        # Skip if window title is empty or generic
        if not window_title or window_title in ['Unknown', 'New Tab']:
            continue
            
        # Get project name
        project_name = activity.get("project_name", "")
        
        # If no project name or it's "general", try to extract it
        if not project_name or project_name == "general":
            project_name = extract_project_name_from_window(window_title, app_name)
        
        # Skip if still no project name
        if not project_name:
            continue
            
        # Normalize timesheet variations
        if 'timesheet' in project_name.lower():
            project_name = 'timesheet'
        
        # Initialize project group if not exists
        if project_name not in project_groups:
            project_groups[project_name] = {
                "project_name": project_name,
                "window_title": project_name,
                "application_name": "Development",
                "duration": 0,
                "duration_hours": 0,
                "file_count": 0,
                "files": set(),
                "applications": set(),
                "subcategory": "development",
                "confidence": 0.9
            }
        
        # Add to project group
        project_groups[project_name]["duration"] += activity["duration"]
        
        # Extract filename if present
        if ' - ' in window_title:
            first_part = window_title.split(' - ')[0]
            # Check if it looks like a filename
            if '.' in first_part and len(first_part.split('.')[-1]) <= 4:
                project_groups[project_name]["files"].add(first_part)
        
        # Track applications
        if app_name:
            project_groups[project_name]["applications"].add(app_name)
    
    # Convert to list
    result = []
    for project_name, group in project_groups.items():
        group["file_count"] = len(group["files"])
        group["duration_hours"] = round(group["duration"] / 3600, 2)
        
        # Format time
        hours = int(group["duration"] // 3600)
        minutes = int((group["duration"] % 3600) // 60)
        
        if hours > 0:
            time_str = f"{hours}h {minutes}m"
        else:
            time_str = f"{minutes}m"
            
        group["window_title"] = f"{project_name} ({group['file_count']} files) - {time_str}"
        
        # Clean up for JSON
        group["files"] = list(group["files"])
        group["applications"] = list(group["applications"])
        
        result.append(group)
    
    return sorted(result, key=lambda x: x["duration"], reverse=True)
```

Then in the main function around line 141, make sure it's calling this function for productive activities:

```python
if category == "productive":
    # Group productive activities by project
    grouped_activities = group_productive_activities_by_project(activities)
    top_activities_by_category[category] = grouped_activities[:10]
else:
    # Original code for other categories...
```
