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

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

  final _supabase = SupabaseService.instance.client;

  Future<List<Map<String, dynamic>>> getEvidencesByIncidentId(
    String incidentId,
  ) async {
    try {
      final response = await _supabase
          .from('evidence')
          .select('*')
          .eq('incident_id', incidentId)
          .order('uploaded_at', ascending: false);

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

  Future<void> addEvidence({
    required String incidentId,
    required String type,
    required String url,
  }) async {
    try {
      await _supabase.from('evidence').insert({
        'id': 'E${DateTime.now().millisecondsSinceEpoch}',
        'incident_id': incidentId,
        'type': type,
        'url': url,
        'uploaded_at': DateTime.now().toIso8601String(),
      });
    } catch (e) {
      throw TExceptions('Failed to add evidence: ${e.toString()}');
    }
  }
}
