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
 
2ca9f67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# server.py

from flask import Flask, request, jsonify, render_template
from app import EasyFarmsAssistant
from conversation_manager import ConversationManager
import logging
from datetime import datetime
import os
from flask_cors import CORS

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize the Flask application
app = Flask(__name__)
CORS(app)

# --- Initialize Core Services ---
try:
    conv_manager = ConversationManager()
    assistant = EasyFarmsAssistant(manager=conv_manager)
    logger.info("EasyFarmsAssistant and ConversationManager initialized successfully.")
except Exception as e:
    logger.error(f"FATAL: Could not initialize services. Error: {e}")
    assistant = None
    conv_manager = None

# --- Frontend Serving Route ---
@app.route('/')
def index():
    """Serves the main chat application page (index.html)."""
    return render_template('index.html')

# --- API Endpoints ---

@app.route('/config', methods=['GET'])
def get_config():
    """Provides public configuration keys to the frontend."""
    return jsonify({
        'imgbb_api_key': os.getenv('IMGBB_API_KEY')
    })

@app.route('/chat', methods=['POST'])
def chat():
    """
    Handles incoming user messages and returns the assistant's response.
    
    Request format:
    {
        "query": "user message text",
        "image_url": "optional image URL",
        "session_id": "optional chat ID for conversation memory",
        "user_id": "user identifier for authentication"
    }
    
    Response format:
    {
        "response": "assistant response",
        "chat_id": "unique chat identifier",
        "is_new_chat": true/false,
        "user_message_id": 1,
        "assistant_message_id": 2,
        "total_messages": 2
    }
    """
    if not assistant:
        return jsonify({"error": "Assistant is not available due to an initialization error."}), 503

    try:
        # Handle both JSON and form data
        if request.is_json:
            data = request.get_json()
            user_message = data.get('query')
            chat_id = data.get('session_id')
            image_url = data.get('image_url')
            user_id = data.get('user_id')
        else:
            # Fallback for form data (backward compatibility)
            data = request.form
            user_message = data.get('message') or data.get('query')
            chat_id = data.get('session_id')
            image_url = data.get('image_url')
            user_id = data.get('user_id')

        # Validate input
        if not user_message and not image_url:
            return jsonify({"error": "Cannot process an empty message. Provide either 'query' text or 'image_url'."}), 400
        
        if not user_id:
            return jsonify({"error": "User ID is required for authentication."}), 400
        
        # Clean up empty/null chat_id
        if chat_id in [None, '', 'null', 'undefined']:
            chat_id = None
        
        # Process the query with the assistant
        result = assistant.process_query(
            user_message=user_message or "",
            user_id=user_id,
            chat_id=chat_id,
            image_url=image_url
        )
        
        # Handle errors from assistant
        if "error" in result:
            return jsonify({
                "error": result["error"],
                "chat_id": result.get("chat_id"),
                "is_new_chat": result.get("is_new_chat", True)
            }), 500
        
        # Return successful response
        return jsonify({
            "response": result["response"],
            "chat_id": result["chat_id"],
            "is_new_chat": result["is_new_chat"],
            "user_message_id": result["user_message_id"],
            "assistant_message_id": result["assistant_message_id"],
            "total_messages": result["total_messages"]
        })
        
    except Exception as e:
        logger.error(f"Error in chat endpoint: {e}")
        return jsonify({"error": f"Server error: {str(e)}"}), 500

@app.route('/chats', methods=['GET'])
def get_all_chats():
    """
    Get all chat sessions with basic information for a specific user.
    
    Query parameters:
    - user_id: Required user identifier
    
    Response format:
    [
        {
            "session_id": "chat_abc123",
            "title": "Crop recommendations for...",
            "message_count": 4,
            "created_at": "2024-01-01T10:00:00",
            "updated_at": "2024-01-01T10:05:00"
        }
    ]
    """
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        user_id = request.args.get('user_id')
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        chats = assistant.get_all_chats(user_id)
        return jsonify(chats)
        
    except Exception as e:
        logger.error(f"Error fetching all chats: {e}")
        return jsonify({"error": "Could not fetch chat list"}), 500

@app.route('/chat/<chat_id>/info', methods=['GET'])
def get_chat_info(chat_id):
    """
    Get information about a specific chat for a specific user.
    
    Query parameters:
    - user_id: Required user identifier
    
    Response format:
    {
        "session_id": "chat_abc123",
        "message_count": 4,
        "created_at": "2024-01-01T10:00:00",
        "updated_at": "2024-01-01T10:05:00",
        "exists": true
    }
    """
    if not assistant:
        return jsonify({"error": "Assistant not available"}), 503
        
    try:
        if not chat_id:
            return jsonify({"error": "Chat ID is required"}), 400
            
        user_id = request.args.get('user_id')
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        info = assistant.get_chat_info(chat_id, user_id)
        return jsonify(info)
        
    except Exception as e:
        logger.error(f"Error getting chat info for {chat_id}: {e}")
        return jsonify({"error": "Could not fetch chat information"}), 500

@app.route('/chat/<chat_id>/messages', methods=['GET'])
def get_chat_messages(chat_id):
    """
    Get all messages for a specific chat and user.
    
    Query parameters:
    - user_id: Required user identifier
    
    Response format:
    [
        {
            "message_id": 1,
            "role": "user",
            "content": "Hello, I need help with farming",
            "timestamp": "2024-01-01T10:00:00",
            "imageUrl": "optional"
        },
        {
            "message_id": 2,
            "role": "assistant",
            "content": "Hello! I'd be happy to help...",
            "timestamp": "2024-01-01T10:00:05"
        }
    ]
    """
    if not assistant:
        return jsonify({"error": "Assistant not available"}), 503
        
    try:
        if not chat_id:
            return jsonify({"error": "Chat ID is required"}), 400
            
        user_id = request.args.get('user_id')
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        messages = assistant.get_messages(chat_id, user_id)
        return jsonify(messages)
        
    except Exception as e:
        logger.error(f"Error fetching messages for chat {chat_id}: {e}")
        return jsonify({"error": "Could not fetch chat messages"}), 500

@app.route('/chat/<chat_id>', methods=['DELETE'])
def delete_chat(chat_id):
    """
    Delete a specific chat and all its messages for a specific user.
    
    Query parameters:
    - user_id: Required user identifier
    
    Response format:
    {
        "status": "success",
        "message": "Chat deleted successfully",
        "chat_id": "chat_abc123"
    }
    """
    if not assistant:
        return jsonify({"error": "Assistant not available"}), 503
        
    try:
        if not chat_id:
            return jsonify({"error": "Chat ID is required"}), 400
            
        user_id = request.args.get('user_id')
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        success = assistant.clear_history(chat_id, user_id)
        
        if success:
            return jsonify({
                "status": "success",
                "message": f"Chat {chat_id} deleted successfully",
                "chat_id": chat_id
            })
        else:
            return jsonify({
                "status": "error",
                "message": f"Failed to delete chat {chat_id}",
                "chat_id": chat_id
            }), 500
            
    except Exception as e:
        logger.error(f"Error deleting chat {chat_id}: {e}")
        return jsonify({"error": "Could not delete chat"}), 500

# --- New User Management Endpoints ---

@app.route('/users/<user_id>/stats', methods=['GET'])
def get_user_stats(user_id):
    """
    Get statistics for a specific user.
    
    Response format:
    {
        "user_id": "user_abc123",
        "total_sessions": 5,
        "total_messages": 42,
        "recent_sessions_24h": 2,
        "average_messages_per_session": 8.4,
        "first_session": "2024-01-01T10:00:00",
        "last_activity": "2024-01-01T15:30:00"
    }
    """
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        stats = conv_manager.get_user_stats(user_id)
        return jsonify(stats)
        
    except Exception as e:
        logger.error(f"Error getting user stats for {user_id}: {e}")
        return jsonify({"error": "Could not fetch user statistics"}), 500

@app.route('/users/<user_id>/chats', methods=['DELETE'])
def delete_all_user_chats(user_id):
    """
    Delete all chats for a specific user.
    
    Response format:
    {
        "status": "success",
        "message": "All chats deleted successfully",
        "user_id": "user_abc123"
    }
    """
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        success = conv_manager.delete_all_user_data(user_id)
        
        if success:
            return jsonify({
                "status": "success",
                "message": f"All chats deleted for user {user_id}",
                "user_id": user_id
            })
        else:
            return jsonify({
                "status": "error",
                "message": f"Failed to delete chats for user {user_id}",
                "user_id": user_id
            }), 500
            
    except Exception as e:
        logger.error(f"Error deleting all chats for user {user_id}: {e}")
        return jsonify({"error": "Could not delete user chats"}), 500

@app.route('/users/<user_id>/export', methods=['GET'])
def export_user_data(user_id):
    """
    Export all data for a specific user.
    
    Response format:
    {
        "user_id": "user_abc123",
        "export_date": "2024-01-01T10:00:00",
        "stats": {...},
        "sessions": [...]
    }
    """
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        # Get user stats and sessions
        stats = conv_manager.get_user_stats(user_id)
        sessions = conv_manager.get_all_chat_sessions(user_id)
        
        # Get all messages for each session
        detailed_sessions = []
        for session in sessions:
            session_data = session.copy()
            messages = conv_manager.get_history(session['session_id'], user_id)
            session_data['messages'] = messages
            detailed_sessions.append(session_data)
        
        export_data = {
            "user_id": user_id,
            "export_date": datetime.utcnow().isoformat(),
            "stats": stats,
            "sessions": detailed_sessions,
            "total_exported_sessions": len(detailed_sessions),
            "total_exported_messages": sum(len(s.get('messages', [])) for s in detailed_sessions)
        }
        
        return jsonify(export_data)
        
    except Exception as e:
        logger.error(f"Error exporting data for user {user_id}: {e}")
        return jsonify({"error": "Could not export user data"}), 500

@app.route('/users/<user_id>', methods=['DELETE'])
def delete_user_account(user_id):
    """
    Delete a user account and all associated data.
    
    Response format:
    {
        "status": "success",
        "message": "User account deleted successfully",
        "user_id": "user_abc123"
    }
    """
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        if not user_id:
            return jsonify({"error": "User ID is required"}), 400
            
        # Delete all user data
        success = conv_manager.delete_all_user_data(user_id)
        
        if success:
            return jsonify({
                "status": "success",
                "message": f"User account {user_id} deleted successfully",
                "user_id": user_id
            })
        else:
            return jsonify({
                "status": "error",
                "message": f"Failed to delete user account {user_id}",
                "user_id": user_id
            }), 500
            
    except Exception as e:
        logger.error(f"Error deleting user account {user_id}: {e}")
        return jsonify({"error": "Could not delete user account"}), 500

# --- Legacy Endpoints for Backward Compatibility ---

@app.route('/history/sessions', methods=['GET'])
def get_sessions():
    """Legacy endpoint - redirects to /chats"""
    return get_all_chats()

@app.route('/history/messages/<session_id>', methods=['GET'])
def get_messages(session_id):
    """Legacy endpoint - redirects to /chat/<session_id>/messages"""
    return get_chat_messages(session_id)

@app.route('/clear', methods=['POST'])
def clear_history():
    """Legacy endpoint for clearing chat history"""
    if not assistant:
        return jsonify({"error": "Assistant is not available."}), 503
    
    try:
        data = request.get_json()
        if not data:
            return jsonify({"error": "Request body is required"}), 400
            
        session_id = data.get('session_id')
        if not session_id:
            return jsonify({"error": "Missing 'session_id' in request body"}), 400

        success = assistant.clear_history(session_id)
        
        if success:
            return jsonify({
                "status": "success",
                "message": f"History for session {session_id} was cleared."
            })
        else:
            return jsonify({"error": "Failed to clear history."}), 500
            
    except Exception as e:
        logger.error(f"Error in legacy clear endpoint: {e}")
        return jsonify({"error": f"Server error: {str(e)}"}), 500

# --- Health Check and Status Endpoints ---

@app.route('/health', methods=['GET'])
def health_check():
    """Health check endpoint for monitoring"""
    try:
        # Check if core services are available
        services_status = {
            "assistant": assistant is not None,
            "conversation_manager": conv_manager is not None,
            "database": False
        }
        
        # Test database connectivity
        if conv_manager:
            try:
                # Try to get chat sessions (will test DB connection)
                conv_manager.get_all_chat_sessions()
                services_status["database"] = True
            except:
                services_status["database"] = False
        
        all_healthy = all(services_status.values())
        
        return jsonify({
            "status": "healthy" if all_healthy else "degraded",
            "services": services_status,
            "timestamp": conv_manager.manager._get_timestamp() if conv_manager else None
        }), 200 if all_healthy else 503
        
    except Exception as e:
        logger.error(f"Health check failed: {e}")
        return jsonify({
            "status": "unhealthy",
            "error": str(e)
        }), 503

@app.route('/stats', methods=['GET'])
def get_stats():
    """Get system statistics"""
    if not conv_manager:
        return jsonify({"error": "Conversation manager not available"}), 503
        
    try:
        chats = assistant.get_all_chats()
        
        total_chats = len(chats)
        total_messages = sum(chat.get('message_count', 0) for chat in chats)
        
        # Recent activity (chats updated in last 24 hours)
        from datetime import datetime, timedelta
        now = datetime.utcnow()
        yesterday = now - timedelta(days=1)
        
        recent_chats = 0
        for chat in chats:
            updated_at = chat.get('updated_at')
            if updated_at:
                try:
                    chat_time = datetime.fromisoformat(updated_at.replace('Z', '+00:00'))
                    if chat_time > yesterday:
                        recent_chats += 1
                except:
                    pass
        
        return jsonify({
            "total_chats": total_chats,
            "total_messages": total_messages,
            "recent_chats_24h": recent_chats,
            "average_messages_per_chat": round(total_messages / total_chats, 2) if total_chats > 0 else 0
        })
        
    except Exception as e:
        logger.error(f"Error getting stats: {e}")
        return jsonify({"error": "Could not fetch statistics"}), 500

# --- Error Handlers ---

@app.errorhandler(404)
def not_found(error):
    """Handle 404 errors"""
    return jsonify({"error": "Endpoint not found"}), 404

@app.errorhandler(500)
def internal_error(error):
    """Handle 500 errors"""
    logger.error(f"Internal server error: {error}")
    return jsonify({"error": "Internal server error"}), 500

# --- Main Execution ---
if __name__ == '__main__':
    # Set debug=False for production
    port = int(os.environ.get('PORT', 7860))
    app.run(debug=False, port=port, host='0.0.0.0')