import 'package:cheminova/models/kyc_model.dart';
import 'package:cheminova/utils/api_urls.dart';
import 'package:dio/dio.dart';

class KycService {

  // Function to fetch KYC data from the API
  Future<List<dynamic>> getKycData(String token) async {
    try {
      // Make a GET request to the KYC API endpoint with authorization token
      var response = await Dio().get(
        ApiUrls.getKycUrl,
       // 'https://api.cnapp.co.in/api/kyc/getAll',
        options: Options(
          headers: {
            'Authorization': 'Bearer $token',
          },
        ),
      );
      // Check if the response status code indicates success
       if (response.statusCode == 200) {
        print("response of ApiUrls.getKycUrl: ${response.data}");
        return response.data; // Return the data as a List<dynamic>
      } else {
        throw Exception("Failed to load KYC data");
      }
    } catch (e) {
      print("Error fetching KYC data: $e");
      return [];
    }
  }

  Future<bool> approveKycStatus(String token, String kycId,String status) async {
    try {
    //  Prepare the payload for approval directly
      var payload = {
        "status": "approved", // Set status to approve
      };

      // Log the payload
    //  print("Payload: $payload");

      // Make a PATCH request to update the KYC status
      var response = await Dio().patch(
        'https://api.cnapp.co.in/api/kyc/update/$kycId', // URL with the KYC ID
        data: payload, // Payload with the status
        options: Options(
          headers: {
            'Authorization': 'Bearer $token',
            'Content-Type': 'application/json',
          },
        ),
      );

      // Check if the response indicates success
      if (response.statusCode == 200) {
        print("KYC status approved successfully: ${response.data}");
        return true; // Return true if the update was successful
      } else {
        print("Failed to approve KYC status: ${response.statusCode} - ${response.data}");
        return false;
      }
    } catch (e) {
      print("Error approving KYC status: ${e}");
      if (e is DioError) {
        // Check if the error has a response
        if (e.response != null) {
          print("Error response data: ${e.response!.data}");
          print("Error response status: ${e.response!.statusCode}");
        }
      }
      return false;
    }
  }
  Future<bool> rejectKycStatus(
      String token, String kycId, String? rejectionReason, String? user, String status) async {
    try {
      // Prepare the payload for the PATCH request
      var data = {
        "status": status, // The KYC status (e.g., "reject")
        "rejectionReason": rejectionReason, // Reason for rejection
        "user": user, // The role of the user (e.g., "Principal Distributer")
        // "notes": [
        //   {
        //     "message": rejectionReason, // Rejection message
        //     "user": user, // User role
        //     "replyDate": DateTime.now().toUtc().toIso8601String(), // Current UTC date for replyDate
        //   }
        // ]
      };

      print("Payload: $data");

      // Make a PATCH request to update the KYC status
      var response = await Dio().patch(
        'https://api.cnapp.co.in/api/kyc/update/$kycId',
        data: data,
        options: Options(
          headers: {
            'Authorization': 'Bearer $token',
            'Content-Type': 'application/json',
          },
        ),
      );

      // Check if the response indicates success
      if (response.statusCode == 200) {
        print("KYC status rejected successfully: ${response.data}");
        return true; // Return true if the update was successful
      } else {
        print("Failed to reject KYC status: ${response.statusCode}");
        return false;
      }
    } catch (e) {
      print("Error rejecting KYC status: ${e}");
      if (e is DioError) {
        // Check if the error has a response
        if (e.response != null) {
          print("Error response data: ${e.response!.data}");
          print("Error response status: ${e.response!.statusCode}");
        }
      }
      return false;
    }
  }
  //
  // Future<bool> rejectKycStatus(String token, String kycId, String? rejectionReason,
  //     PrincipalDistributer? user, String status) async {
  //   try {
  //     // Prepare the payload with proper rejectionReason structure
  //     var data = {
  //       "status": status,
  //       "rejectionReason": rejectionReason,
  //       "user": user,
  //     };
  //
  //     print("Payload: $data");
  //
  //     // Make a PATCH request to update the KYC status
  //     var response = await Dio().patch(
  //       'https://api.cnapp.co.in/api/kyc/update/$kycId',
  //       data: data,
  //       options: Options(
  //         headers: {
  //           'Authorization': 'Bearer $token',
  //           'Content-Type': 'application/json',
  //         },
  //       ),
  //     );
  //
  //     // Check if the response indicates success
  //     if (response.statusCode == 200) {
  //       print("KYC status rejected successfully: ${response.data}");
  //       return true; // Return true if the update was successful
  //     } else {
  //       print("Failed to reject KYC status: ${response.statusCode}");
  //       return false;
  //     }
  //   } catch (e) {
  //     print("Error rejecting KYC status: ${e}");
  //     if (e is DioError) {
  //       // Check if the error has a response
  //       if (e.response != null) {
  //         print("Error response data: ${e.response!.data}");
  //         print("Error response status: ${e.response!.statusCode}");
  //       }
  //     }
  //     return false;
  //   }
  // }

//
  // Future<bool> updateKycStatus(String token, String kycId,
  //     {List<dynamic>? rejectionReason, PrincipalDistributer? user}) async {
  //   try {
  //     // Prepare the payload based on the action (approve or reject)
  //   var data;
  //
  //
  //       // Payload for approve action
  //
  //       data = {
  //         "status": "reject", // Set status to reject
  //         "rejectionReason": rejectionReason, // Pass the rejection reason
  //         "user": user, // Pass the user (e.g., 'Principal Distributer')
  //       };
  //
  //     else {
  //       throw Exception("Invalid action: must be 'approve' or 'reject'");
  //     }
  //
  //     // Make a PATCH request to update the KYC status
  //     var response = await Dio().patch(
  //       'https://api.cnapp.co.in/api/kyc/update/$kycId', // URL with the KYC ID
  //       data: data, // Payload with the status
  //       options: Options(
  //         headers: {
  //           'Authorization': 'Bearer $token',
  //           'Content-Type': 'application/json',
  //         },
  //       ),
  //     );
  //
  //     // Check if the response indicates success
  //     if (response.statusCode == 200) {
  //       print("KYC status updated successfully: ${response.data}");
  //       return true; // Return true if the update was successful
  //     } else {
  //       print("Failed to update KYC status: ${response.statusCode}");
  //       return false;
  //     }
  //   } catch (e) {
  //     print("Error updating KYC status: $e");
  //     return false;
  //   }
  // }

}


