// import axios from 'axios';

// class DashboardService {

//   constructor() {
//     this.baseURL = 'https://your-api-url';  // Set your base URL for API
//     this.getAllURL = '/get_module_id';
//     this.addDataURl = '/Savedata';
//     this.deleteFieldURL = '/delete_by_header_id';
//     this.getbyidURL = '/get_dashboard_headerbyid';
//     this.editURL = '/update_Dashbord1_Line';
//     this.updateURL = '/update_Dashbord1_Lineby_id';
//   }

//   // Get all dashboards
//   getAllDash() {
//     return axios.get(`${this.baseURL}/get_Dashboard_header`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching all dashboards:', error);
//         throw error;
//       });
//   }

//   // Get dashboards by module ID
//   getAllByModuleId(module_id, page = 0, size = 1000) {
//     return axios.get(`${this.baseURL}${this.getAllURL}`, {
//       params: {
//         page,
//         size,
//         module_id
//       }
//     })
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching dashboards by module ID:', error);
//         throw error;
//       });
//   }

//   // Create new dashboard
//   create(data) {
//     return axios.post(`${this.baseURL}${this.addDataURl}`, data)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error creating dashboard:', error);
//         throw error;
//       });
//   }

//   // Delete dashboard field
//   deleteField(id) {
//     return axios.delete(`${this.baseURL}${this.deleteFieldURL}/${id}`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error deleting dashboard field:', error);
//         throw error;
//       });
//   }

//   // Get dashboard by ID
//   getById(id) {
//     return axios.get(`${this.baseURL}${this.getbyidURL}/${id}`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching dashboard by ID:', error);
//         throw error;
//       });
//   }

//   // Add or Edit dashboard
//   addToDB(line) {
//     return axios.put(`${this.baseURL}${this.editURL}`, line)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error updating dashboard line:', error);
//         throw error;
//       });
//   }

//   // Update dashboard line data by ID
//   updateLineData(id, line) {
//     return axios.put(`${this.baseURL}${this.updateURL}/${id}`, line)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error updating dashboard line data:', error);
//         throw error;
//       });
//   }

//   // Get dashboard count by module ID
//   getCount(moduleId) {
//     return axios.get(`${this.baseURL}/get_dashboard/${moduleId}`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching dashboard count:', error);
//         throw error;
//       });
//   }

//   // Update dashboard header
//   updateDash(dashboardHeader) {
//     return axios.put(`${this.baseURL}/update_dashboard_header`, dashboardHeader)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error updating dashboard header:', error);
//         throw error;
//       });
//   }

//   // Schedule methods
//   saveData(data) {
//     return axios.post(`${this.baseURL}/DashboardSchedule/DashboardSchedule`, data)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error saving schedule data:', error);
//         throw error;
//       });
//   }

//   getDetails() {
//     return axios.get(`${this.baseURL}/DashboardSchedule/DashboardSchedule`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching schedule details:', error);
//         throw error;
//       });
//   }

//   getDetailsById(id) {
//     return axios.get(`${this.baseURL}/DashboardSchedule/DashboardSchedule/${id}`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error fetching schedule details by ID:', error);
//         throw error;
//       });
//   }

//   deleteById(id) {
//     return axios.delete(`${this.baseURL}/DashboardSchedule/DashboardSchedule/${id}`)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error deleting schedule by ID:', error);
//         throw error;
//       });
//   }

//   updateData(data, id) {
//     return axios.put(`${this.baseURL}/DashboardSchedule/DashboardSchedule/${id}`, data)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error updating schedule data:', error);
//         throw error;
//       });
//   }

//   // Toggle functionality
//   updateToggle(value) {
//     // Logic for toggle update (e.g. update a state, or some other action)
//     console.log('Toggle updated to:', value);
//   }
// }

// export default new DashboardService();


import apiService from '../APIRequestService/APIService';

class DashboardService {
  constructor() {
    this.getAllURL = '/get_module_id';
    this.addDataURL = '/Savedata';
    this.deleteFieldURL = '/delete_by_header_id';
    this.getByIdURL = '/get_dashboard_headerbyid';
    this.editURL = '/update_Dashbord1_Line';
    this.updateURL = '/update_Dashbord1_Lineby_id';
  }

  // Get all dashboards
  // getAllDash() {
  //   return apiService.get('/get_Dashboard_header')
  //     .then(response => response.data)
      
  //     .catch(error => {
  //       console.error('Error fetching all dashboards:', error);
  //       throw error;
  //     });
  // }

  async getAllDash() {
    try {
      const response = await apiService.get('/get_Dashboard_header');
      console.log('Service Response:', response);
      if (!response || !response.data) {
        throw new Error('Unexpected API response format.');
      }
      return response.data;
    } catch (error) {
      console.error('Error fetching dashboards:', error);
      throw new Error('Failed to fetch dashboards. Please try again later.');
    }
  }
  

  // Get dashboards by module ID
  getAllByModuleId(module_id, page = 0, size = 1000) {
    return apiService.get(this.getAllURL, {
      params: { page, size, module_id }
    })
      .then(response => response.data)
      .catch(error => {
        console.error('Error fetching dashboards by module ID:', error);
        throw error;
      });
  }

  // Create new dashboard
  create(data) {
    return apiService.post(this.addDataURL, data)
      .then(response => response.data)
      .catch(error => {
        console.error('Error creating dashboard:', error);
        throw error;
      });
  }

  // Delete dashboard field
  deleteField(id) {
    return apiService.delete(`${this.deleteFieldURL}/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error deleting dashboard field:', error);
        throw error;
      });
  }

  // Get dashboard by ID
  getById(id) {
    return apiService.get(`${this.getByIdURL}/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error fetching dashboard by ID:', error);
        throw error;
      });
  }

  // Add or Edit dashboard
  addToDB(line) {
    return apiService.put(this.editURL, line)
      .then(response => response.data)
      .catch(error => {
        console.error('Error updating dashboard line:', error);
        throw error;
      });
  }

  // Update dashboard line data by ID
  updateLineData(id, line) {
    return apiService.put(`${this.updateURL}/${id}`, line)
      .then(response => response.data)
      .catch(error => {
        console.error('Error updating dashboard line data:', error);
        throw error;
      });
  }

  // Get dashboard count by module ID
  getCount(moduleId) {
    return apiService.get(`/get_dashboard/${moduleId}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error fetching dashboard count:', error);
        throw error;
      });
  }

  // Update dashboard header
  updateDash(dashboardHeader) {
    return apiService.put('/update_dashboard_header', dashboardHeader)
      .then(response => response.data)
      .catch(error => {
        console.error('Error updating dashboard header:', error);
        throw error;
      });
  }

  // Schedule methods
  saveData(data) {
    return apiService.post('/DashboardSchedule/DashboardSchedule', data)
      .then(response => response.data)
      .catch(error => {
        console.error('Error saving schedule data:', error);
        throw error;
      });
  }

  getDetails() {
    return apiService.get('/DashboardSchedule/DashboardSchedule')
      .then(response => response.data)
      .catch(error => {
        console.error('Error fetching schedule details:', error);
        throw error;
      });
  }

  getDetailsById(id) {
    return apiService.get(`/DashboardSchedule/DashboardSchedule/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error fetching schedule details by ID:', error);
        throw error;
      });
  }

  deleteById(id) {
    return apiService.delete(`/DashboardSchedule/DashboardSchedule/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error deleting schedule by ID:', error);
        throw error;
      });
  }

  updateData(data, id) {
    return apiService.put(`/DashboardSchedule/DashboardSchedule/${id}`, data)
      .then(response => response.data)
      .catch(error => {
        console.error('Error updating schedule data:', error);
        throw error;
      });
  }

  // Toggle functionality
  updateToggle(value) {
    console.log('Toggle updated to:', value);
  }
}

export default new DashboardService();
