# URGENT FIX NEEDED for blog_routes.py

The file `blog_routes.py` has multiple endpoints that are crashing or have syntax errors. Please apply these fixes manually.

## 1. Fix `get_blogs_by_category` (around line 490)

**Problem**: It passes a list to `include_legacy_fields`, causing a crash.
**Fix**: Change `include_legacy_fields=blogs` to `include_legacy_fields=True`.

```python
        # Return raw data - let the @encrypt_response decorator handle Response creation
        return format_api_response(
            data={'blogs': blogs},
            message="Blogs by category retrieved successfully",
            include_legacy_fields=True  # FIXED: Must be True
        )
```

## 2. Fix `search_blogs` (around line 570)

**Problem**: It passes a list to `include_legacy_fields`, causing a crash.
**Fix**: Change `include_legacy_fields=blogs` to `include_legacy_fields=True`.

```python
        # Return raw data - let the @encrypt_response decorator handle Response creation
        return format_api_response(
            data={'blogs': blogs},
            message="Blog search results retrieved successfully",
            include_legacy_fields=True  # FIXED: Must be True
        )
```

## 3. Fix `get_categories` (around line 650)

**Problem**: Syntax error (missing except block connection) or wrong return value.
**Fix**: Ensure the structure is correct:

```python
@blog_bp.route('/api/blogs/categories', methods=['GET'])
@require_api_key
@encrypt_response
def get_categories():
    # ... (code to fetch categories) ...
        finally:
            # Always clean up resources
            if cursor and hasattr(cursor, 'close'):
                cursor.close()
            if conn and hasattr(conn, 'close'):
                conn.close()
        
        # Log successful response
        logger.info(f"GET /api/blogs/categories - Success: {len(categories)} categories returned")
        
        return format_api_response(
            data={'categories': categories},
            message="Categories retrieved successfully",
            include_legacy_fields=True  # FIXED: Must be True
        )
    except Exception as e:
        logger.error("Error fetching blog categories: {}".format(str(e)))
        return create_error_response(str(e), status_code=500, encrypt=True)
```

## 4. Fix `get_popular_tags` (around line 695)

**Problem**: Passes list to `include_legacy_fields`.
**Fix**: Change `include_legacy_fields=popular_tags` to `include_legacy_fields=True`.

```python
        return format_api_response(
            data={'tags': popular_tags},
            message="Popular tags retrieved successfully",
            include_legacy_fields=True  # FIXED: Must be True
        )
```

## 5. Fix `get_trending_posts` (around line 780)

**Problem**: Passes list to `include_legacy_fields`.
**Fix**: Change `include_legacy_fields=trending_posts` to `include_legacy_fields=True`.

```python
        return format_api_response(
            data={'trending_posts': trending_posts},
            message="Trending posts retrieved successfully",
            include_legacy_fields=True  # FIXED: Must be True
        )
```

## 6. Fix `track_blog_view` (around line 794)

**Problem**: Passes dict to `include_legacy_fields`.
**Fix**: Change `include_legacy_fields=cors_response` to `include_legacy_fields=True`.

```python
        return format_api_response(cors_response, include_legacy_fields=True), 200, {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'POST, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type'
        }
```

## SUMMARY
Search for `include_legacy_fields=` and ensure it is ALWAYS set to `True` (boolean), never a variable name like `blogs`, `categories`, etc.
