import apiService from '../APIRequestService/APIService';
import { saveAs } from 'file-saver';


// Function to run a specific report
export const runReport = async (reportId) => {
  try {
    const response = await apiService.get(`/Rpt_builder2/Rpt_builder2/${reportId}`); // Using the reportId in the URL
    console.log("Run Report response:", response);
    return response; // Return the response to be handled in the calling component
  } catch (error) {
    console.error("Error while running the report:", error);
    throw error; // Re-throw the error to handle it in the calling component
  }
};

// Function to fetch all reports
export const fetchAllReportsApi = async () => {
  console.log("report runner on ")
  
  try {
    const response = await apiService.get('/Rpt_builder2/Rpt_builder2'); // API call to fetch all reports
    console.log("Fetch all reports response:", response.data);
    return response.data; // Assuming response data comes in `data` field
  } catch (error) {
    console.error("Error while fetching all reports:", error);
    throw error; // Re-throw the error for handling in the calling component
  }
};


export const fetchStandardParameters = async (url) =>{
    try {
      const response = await apiService.get(`Rpt_builder2_lines/fetch_data_url?url=${url}`)
      console.log("response data ",response.data);
      return response.data;
    } catch (error) {
      console.error("Error while fetching all standard parameter:", error);
    }
}

// file download api

export const downloadFile = (format, dataList, name) => {
  const url = `/rbbuilder/fileconverter/downloadFile/${format}`;
  
  apiService.downloadFile(url, dataList).subscribe(
    (response) => {
      saveAs(response, `${name}.${format}`);
    },
    (error) => {
      console.error('Error downloading file:', error);
    }
  );
};





