// userGroupMaintenanceAPI.js
import  apiService from '../../src/APIRequestService/APIService';
import {toast} from 'react-toastify';

export const fetchUserGroups = async () => {
      try {
        const response = await apiService.get('api/getAllUsrGrp'); // API endpoint
        console.log(response.data);
        return response.data; // Assuming data is returned here
      } catch (error) {
        console.error('Error fetching user groups:', error);
        throw error;
      }
  
     
  };
  
  export const createUserGroup = async (payload) => {
    console.log("Payload being sent:", JSON.stringify(payload, null, 2)); // Pretty print payload for better visibility
  
    try {
      // Call the API with custom headers
      const response = await apiService.post('/api/addOneUsrGrp', payload, {
        headers: {
          "Content-Type": "application/json", // Explicitly set content type
        },
      });
  
      console.log("Response Data:", response.data);
      return response;
  
    } catch (error) {
      if (error.response) {
        // Log response details for server errors
        console.error("Server responded with an error:", error.response.data);
        console.error("Status Code:", error.response.status);
        console.error("Headers:", error.response.headers);
  
        if (error.response.status >= 500) {
          toast.error("Server error occurred. Please try again later.");
        } else {
          toast.error(`Request failed: ${error.response.data.message || error.response.statusText}`);
        }
  
      } else if (error.request) {
        // Log request details for network errors
        console.error("No response received from server:", error.request);
        toast.error("Server unreachable. Please check your network or contact support.");
      } else {
        // Log generic errors
        console.error("Error setting up the request:", error.message);
        toast.error("Unexpected error occurred. Please try again.");
      }
  
      throw error; // Rethrow the error after logging for further handling if needed
    }
  }
  

//   export const createUserGroup = async (payload) => {
//     console.log("Payload being sent:", payload);
//     try {
//         const response = await apiService.post('/api/addOneUsrGrp', payload);
//         console.log(response.data);
//         return response;
//     } catch (error) {
//         console.error('Error fetching user groups:', error.response );
//     }
// };


  export const  updateUserGroup = async (id, payload) => {
    try {
        const response = await apiService.post(`/api/updateOneUsrGrp`, payload);
        console.log(response.data);
        return response;

    } catch (error) {
        console.error('Error fetching user groups:', error);
    }
  };


  export const  deleteUserGroup = async (id) => {
    try {
        const response = await apiService.delete(`/api/delete_usr/${id}`);
        console.log(response.data);
        return response;

    } catch (error) {
        console.error('Error fetching user groups:', error);
    }
  };

  