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

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

  final _supabase = SupabaseService.instance.client;

  Future<List<Map<String, dynamic>>> getUpcomingEvents() async {
    try {
      final now = DateTime.now().toIso8601String();
      final response = await _supabase
          .from('events')
          .select('*')
          .gte('end_date', now)
          .order('start_date', ascending: true);

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

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

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