import 'package:get/get.dart';
import 'package:sigap/src/cores/services/supabase_service.dart';
import 'package:sigap/src/utils/exceptions/exceptions.dart';

class CrimeIncidentsRepository extends GetxController {
  static CrimeIncidentsRepository get instance => Get.find();

  final _supabase = SupabaseService.instance.client;

  Future<List<Map<String, dynamic>>> getRecentIncidents() async {
    try {
      final response = await _supabase
          .from('incident_logs')
          .select('*, location_id(*), category_id(*)')
          .order('time', ascending: false)
          .limit(10);

      return List<Map<String, dynamic>>.from(response);
    } catch (e) {
      throw TExceptions('Failed to fetch recent incidents: ${e.toString()}');
    }
  }

  Future<Map<String, dynamic>> getIncidentStatistics() async {
    try {
      final response = await _supabase.rpc('get_incident_statistics');
      return response as Map<String, dynamic>;
    } catch (e) {
      throw TExceptions('Failed to fetch incident statistics: ${e.toString()}');
    }
  }
}
