import 'dart:convert'; import 'package:http/http.dart' as http; /// Provides AI-powered activity suggestions using an external endpoint. /// /// The endpoint is expected to accept POST JSON: /// { "city": "...", "interests": ["food","art"], "budget": "low|mid|high" } /// and return { "suggestions": [ { "title": "...", "detail": "..." }, ... ] } /// /// This class is a thin client and can be wired to a custom backend that /// proxies LLM calls (to avoid shipping secrets in the app). class AiActivityService { /// Base URL of the AI suggestion endpoint. final String baseUrl; /// Optional API key if your backend requires it. final String? apiKey; /// Creates an AI activity service client. const AiActivityService({required this.baseUrl, this.apiKey}); /// Fetches suggestions for the given [city] with optional [interests] and [budget]. /// /// Returns a list of string suggestions. In case of error, returns an empty list /// to keep the UI responsive. Future> fetchSuggestions({ required String city, List interests = const [], String budget = 'mid', }) async { final uri = Uri.parse('$baseUrl/ai/suggestions'); try { final response = await http.post( uri, headers: { 'Content-Type': 'application/json', if (apiKey != null) 'Authorization': 'Bearer $apiKey', }, body: json.encode({ 'city': city, 'interests': interests, 'budget': budget, }), ); if (response.statusCode == 200) { final data = json.decode(response.body) as Map; final list = (data['suggestions'] as List? ?? []) .cast>(); return list .map((item) => item['title'] as String? ?? 'Suggestion') .toList(); } return const []; } catch (_) { return const []; } } }