import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

import '../../Models/hrmModels/leaveApplicationDetailsResponse.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class LeaveApplicationDetailsProvider extends ChangeNotifier {
  leaveApplicationDetailsResponse? _response;
  bool _isLoading = false;
  String? _errorMessage;

  leaveApplicationDetailsResponse? get response => _response;
  bool get isLoading => _isLoading;
  String? get errorMessage => _errorMessage;

  /// Fetch leave application details
  Future<void> fetchLeaveApplicationDetails(BuildContext context, String leaveRequestId) async {
    _isLoading = true;
    _errorMessage = null;
    _response = null;
    notifyListeners();

    try {
      final provider = Provider.of<HomescreenNotifier>(context, listen: false);
      final result = await ApiCalling.leaveApplicationDetailAPI(
        provider.session,
        provider.empId,
        leaveRequestId,
      );

      if (result != null) {
        _response = result;
      } else {
        _errorMessage = "No data found!";
      }
    } catch (e) {
      _errorMessage = "Something went wrong: $e";
      debugPrint('Error fetching leave application details: $e');
    }

    _isLoading = false;
    notifyListeners();
  }

  /// Clear the current response data
  void clearData() {
    _response = null;
    _errorMessage = null;
    notifyListeners();
  }
}