import 'dart:async';
import '../../models/leave_request.dart';
import '../../models/advance_request.dart';

class RequestService {
  // Singleton implementation
  static final RequestService _instance = RequestService._internal();
  factory RequestService() => _instance;
  RequestService._internal();

  // Initialize as empty lists (this should already be working, but let's make it explicit)
  final List<LeaveRequest> _leaveRequests = <LeaveRequest>[];
  final List<AdvanceRequest> _advanceRequests = <AdvanceRequest>[];
  
  // Stream controllers to notify listeners of changes
  final _leaveRequestsController = StreamController<List<LeaveRequest>>.broadcast();
  final _advanceRequestsController = StreamController<List<AdvanceRequest>>.broadcast();
  
  // Stream getters
  Stream<List<LeaveRequest>> get leaveRequestsStream => _leaveRequestsController.stream;
  Stream<List<AdvanceRequest>> get advanceRequestsStream => _advanceRequestsController.stream;

  // Get all leave requests as a Future
  Future<List<LeaveRequest>> getLeaveRequests() async {
    return List.from(_leaveRequests);
  }

  // Get all advance requests as a Future
  Future<List<AdvanceRequest>> getAdvanceRequests() async {
    return List.from(_advanceRequests);
  }

  // Add a new leave request
  Future<void> addLeaveRequest(LeaveRequest request) async {
    _leaveRequests.add(request);
    _leaveRequestsController.add(List.from(_leaveRequests));
  }

  // Add a new advance request
  Future<void> addAdvanceRequest(AdvanceRequest request) async {
    _advanceRequests.add(request);
    _advanceRequestsController.add(List.from(_advanceRequests));
  }

  // Update leave request status
  Future<void> updateLeaveRequestStatus(String id, String status) async {
    final index = _leaveRequests.indexWhere((request) => request.id == id);
    if (index != -1) {
      final updatedRequest = LeaveRequest(
        id: _leaveRequests[index].id,
        leaveType: _leaveRequests[index].leaveType,
        isTimedLeave: _leaveRequests[index].isTimedLeave,
        fromDate: _leaveRequests[index].fromDate,
        toDate: _leaveRequests[index].toDate,
        fromTime: _leaveRequests[index].fromTime,
        toTime: _leaveRequests[index].toTime,
        reason: _leaveRequests[index].reason,
        requestDate: _leaveRequests[index].requestDate,
        status: status,
      );
      _leaveRequests[index] = updatedRequest;
      _leaveRequestsController.add(List.from(_leaveRequests));
    }
  }

  // Update advance request status
  Future<void> updateAdvanceRequestStatus(String id, String status) async {
    final index = _advanceRequests.indexWhere((request) => request.id == id);
    if (index != -1) {
      final updatedRequest = AdvanceRequest(
        id: _advanceRequests[index].id,
        amount: _advanceRequests[index].amount,
        reason: _advanceRequests[index].reason,
        status: status,
      );
      _advanceRequests[index] = updatedRequest;
      _advanceRequestsController.add(List.from(_advanceRequests));
    }
  }
  
  // Dispose method to close streams
  void dispose() {
    _leaveRequestsController.close();
    _advanceRequestsController.close();
  }
}