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

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

  final _supabase = SupabaseService.instance.client;

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

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

  Future<Map<String, dynamic>> getIncidentById(String id) async {
    try {
      final response =
          await _supabase
              .from('incident_logs')
              .select('*, location_id(*), category_id(*)')
              .eq('id', id)
              .single();

      return response;
    } catch (e) {
      throw TExceptions('Failed to fetch incident details: ${e.toString()}');
    }
  }
}
