import apiService from '../APIRequestService/APIServices';
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}`);
    return response.data;
  } catch (error) {
    console.error("Error running report:", error);
    throw error;
  }
};

// Function to fetch all reports
export const fetchAllReportsApi = async () => {
  try {
    const response = await apiService.get('Rpt_builder2/Rpt_builder2');
    return response.data;
  } catch (error) {
    console.error("Error fetching reports:", error);
    throw error;
  }
};

// Function to fetch standard parameters
export const fetchStandardParameters = async (url) => {
  try {
    const response = await apiService.get(`Rpt_builder2_lines/fetch_data_url?url=${encodeURIComponent(url)}`);
    return response.data;
  } catch (error) {
    console.error("Error fetching parameters:", error);
    throw error;
  }
};

// File download function
export const downloadFile = async (format, dataList, name) => {
  try {
    const blob = await apiService.downloadFile(
      `rbbuilder/fileconverter/downloadFile/${format}`,
      dataList
    );
    saveAs(blob, `${name}.${format}`);
  } catch (error) {
    console.error('Error downloading file:', error);
    throw error;
  }
};