import 'dart:convert';
import 'package:http/http.dart' as http;

class GroqService {
  static const String _apiKey = 'YOUR_API_KEY';

  static Future<String> fetchIngredients(String query) async {
    final res = await http.post(
      Uri.parse('https://api.groq.com/openai/v1/chat/completions'),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $_apiKey',
      },
      body: jsonEncode({
        'model': 'llama-3.1-8b-instant',
        'max_tokens': 600,
        'messages': [
          {
            'role': 'system',
            'content':
            'Return ONLY JSON array of grocery items...'
          },
          {'role': 'user', 'content': query}
        ],
      }),
    );

    final data = jsonDecode(res.body);
    return data['choices'][0]['message']['content'];
  }
}


class AiSearchService {
  static const String baseUrl =
      'http://10.0.2.2:8000/find-ingredients';

  Future<Map<String, dynamic>> search(String query) async {
    final response = await http.post(
      Uri.parse(baseUrl),
      headers: {
        'Content-Type': 'application/json',
      },
      body: jsonEncode({
        "user_input": query,
        "customer_id": 6060,
        "latitude": 11.0050728,
        "longitude": 76.9508513
      }),
    );

    if (response.statusCode == 200) {
      return jsonDecode(response.body);
    }

    throw Exception("Failed to load results");
  }
}