diff --git a/lib/ai_agent/ai_service.dart b/lib/ai_agent/ai_service.dart index 978530d..7467145 100644 --- a/lib/ai_agent/ai_service.dart +++ b/lib/ai_agent/ai_service.dart @@ -1,5 +1,7 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/foundation.dart'; import 'package:connect/config/app_config.dart'; import 'package:connect/services/points_service.dart'; @@ -154,18 +156,33 @@ Always be encouraging and help users get the most out of Connect! /// Saves conversation history to Firestore for analytics and improvement. /// - /// This method can be implemented to store user interactions with Dino, + /// Stores user interactions with Dino to the 'ai_conversations' collection, /// which helps analyze user needs and improve AI responses over time. /// /// [userId] The ID of the user having the conversation /// [userMessage] The message sent by the user /// [aiResponse] The response generated by the AI /// - /// Example implementation would save to a 'conversations' collection - /// in Firestore with timestamp and user context. + /// Saves to the 'ai_conversations' collection in Firestore with timestamp. static Future saveConversation(String userId, String userMessage, String aiResponse) async { - // You can implement this to save conversations for analytics - // This helps improve the AI responses over time + // Validate input parameters + if (userId.isEmpty || userMessage.trim().isEmpty || aiResponse.trim().isEmpty) { + debugPrint('Cannot save conversation: Invalid parameters provided'); + return; + } + + try { + await FirebaseFirestore.instance + .collection('ai_conversations') + .add({ + 'userId': userId, + 'userMessage': userMessage, + 'aiResponse': aiResponse, + 'timestamp': FieldValue.serverTimestamp(), + }); + } catch (e) { + debugPrint('Failed to save conversation to Firestore: $e'); + } } /// Gets a list of suggested questions users can ask Dino.