import 'package:get/get.dart';
import 'package:sigap/src/cores/services/supabase_service.dart';
import 'package:sigap/src/features/daily-ops/data/models/models/patrol_units_model.dart';
import 'package:sigap/src/utils/exceptions/exceptions.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

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

  final _supabase = SupabaseService.instance.client;

  // Get all patrol units
  Future<List<PatrolUnitModel>> getAllPatrolUnits() async {
    try {
      final patrolUnits = await _supabase
          .from('patrol_units')
          .select('*, members:officers(*)')
          .order('name');
      
      return patrolUnits.map((unit) => PatrolUnitModel.fromJson(unit)).toList();
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to fetch patrol units: ${e.toString()}');
    }
  }

  // Get patrol unit by ID
  Future<PatrolUnitModel> getPatrolUnitById(String id) async {
    try {
      final patrolUnit = await _supabase
          .from('patrol_units')
          .select('*, members:officers(*)')
          .eq('id', id)
          .single();
          
      return PatrolUnitModel.fromJson(patrolUnit);
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to fetch patrol unit data: ${e.toString()}');
    }
  }

  // Get patrol units by unit ID
  Future<List<PatrolUnitModel>> getPatrolUnitsByUnitId(String unitId) async {
    try {
      final patrolUnits = await _supabase
          .from('patrol_units')
          .select('*, members:officers(*)')
          .eq('unit_id', unitId)
          .order('name');
      
      return patrolUnits.map((unit) => PatrolUnitModel.fromJson(unit)).toList();
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to fetch patrol units: ${e.toString()}');
    }
  }

  // Get patrol units by status
  Future<List<PatrolUnitModel>> getPatrolUnitsByStatus(String status) async {
    try {
      final patrolUnits = await _supabase
          .from('patrol_units')
          .select('*, members:officers(*)')
          .eq('status', status)
          .order('name');
      
      return patrolUnits.map((unit) => PatrolUnitModel.fromJson(unit)).toList();
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to fetch patrol units: ${e.toString()}');
    }
  }

  // Get patrol units by type
  Future<List<PatrolUnitModel>> getPatrolUnitsByType(String type) async {
    try {
      final patrolUnits = await _supabase
          .from('patrol_units')
          .select('*, members:officers(*)')
          .eq('type', type)
          .order('name');
      
      return patrolUnits.map((unit) => PatrolUnitModel.fromJson(unit)).toList();
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to fetch patrol units: ${e.toString()}');
    }
  }

  // Update patrol unit status
  Future<void> updatePatrolUnitStatus(String id, String status) async {
    try {
      await _supabase
          .from('patrol_units')
          .update({'status': status})
          .eq('id', id);
    } on PostgrestException catch (error) {
      throw TExceptions.fromCode(error.code ?? 'unknown_error');
    } catch (e) {
      throw TExceptions('Failed to update patrol unit status: ${e.toString()}');
    }
  }
}
