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

const API_BASE_URL = 'api';

const ReportBuilderService = {
  getById: (id) => apiService.get(`${API_BASE_URL}/edit-report/${id}`),

  buildReport: (id) => {
    const params = { id };
    return apiService.get(`${API_BASE_URL}/build_report`, params);
  },

  getAll: (moduleId, page = 0, size = 1000) => {
    const params = { page, size, moduleId };
    return apiService.get(`${API_BASE_URL}/report-builder-by-id`, params);
  },

  create: (fbHeader, moduleId) => {
    const params = { moduleId };
    return apiService.post(`${API_BASE_URL}/report-builder`, fbHeader, { params });
  },

  createservicereport: (fbHeader, moduleId) => {
    const params = { moduleId };
    return apiService.post(`${API_BASE_URL}/report-builder_service`, fbHeader, { params });
  },

  createQuery: (reportId) => {
    const params = { reportId };
    return apiService.post(`${API_BASE_URL}/add-master-query`, {}, { params });
  },

  update: (id, functionRegister) => {
    return apiService.put(`${API_BASE_URL}/add-master-query/${id}`, functionRegister);
  },

  updateservicereport: (id, functionRegister) => {
    return apiService.put(`${API_BASE_URL}/updatereport/${id}`, functionRegister);
  },

  getMasterQuery: (id) => apiService.get(`${API_BASE_URL}/master-query/${id}`),

  getMasterData: (query) => {
    const params = { sql_query: query };
    return apiService.get(`${API_BASE_URL}/master-query-data`, params);
  },

  report2: (serviceName) => apiService.post(`${API_BASE_URL}/add-report`, serviceName),

  saveq: (data) => apiService.post('FndQuery/query', data),

  getall: () => apiService.get('FndQuery/query'),

  getreportdata: (apiName) => {
    const url = `${API_BASE_URL}/${apiName}`;
    return apiService.get(url);
  },

  getDatabase: () => apiService.get('SqlworkbenchSqlcont/sql'),

  getTableListn: (val) => apiService.get(`Table_list/${val}`),  // table list

  getcolListn: (val, val1) => apiService.get(`Table_list/${val}/${val1}`), // column list

  getColumnList: (tableSchema, tables) => {
    const params = { str: tables.join(',') };
    return apiService.get(`${API_BASE_URL}/AllTable_list/${tableSchema}`, { params });
  },

  getAllColumnsFromAllTables: (tableNames) => {
    // If tableNames is an array, join it with commas
    const tables = Array.isArray(tableNames) ? tableNames.join(',') : tableNames;
    return apiService.get(`Alias_Table_list/${tables}`);
  },

  getcollist: (table) => apiService.get(`fndMenu/loadcolumn/${table}`),

  createdb: (data) => apiService.post('SqlworkbenchSqlcont/sql', data),

  updateSqlModel: (id, sqlModel) => apiService.put(`SqlworkbenchSqlcont/sql/${id}`, sqlModel),

  getSqlModelById: (id) => apiService.get(`SqlworkbenchSqlcont/sql/${id}`),

  deleteSqlModel: (id) => apiService.delete(`SqlworkbenchSqlcont/sql/${id}`),

  getallentity: () => apiService.get(`${API_BASE_URL}/report-builder`),

  saveData: (data) => apiService.post('Rpt_builder/Rpt_builder', data),

  getDetails: () => apiService.get('Rpt_builder/Rpt_builder'),

  getDetailsById: (id) => apiService.get(`Rpt_builder/Rpt_builder/${id}`),

  deleteById: (id) => apiService.delete(`Rpt_builder/Rpt_builder/${id}`),

  updateData: (data, id) => apiService.put(`Rpt_builder/Rpt_builder/${id}`, data),

  saverbData: (data) => apiService.post('Rpt_builder2/Rpt_builder2', data),

  getrbDetails: () => apiService.get('Rpt_builder2/Rpt_builder2'),

  getrbDetailsById: (id) => apiService.get(`Rpt_builder2/Rpt_builder2/${id}`),

  deletrbById: (id) => apiService.delete(`Rpt_builder2/Rpt_builder2/${id}`),

  updaterbData: (data, id) => apiService.put(`Rpt_builder2/Rpt_builder2/${id}`, data),

  updaterbLineData: (data, id) => apiService.put(`Rpt_builder2_lines/update/${id}`, data),

  getrbLineDetailsById: (id) => apiService.get(`Rpt_builder2_lines/Rpt_builder2_lines/${id}`),

  getStdParamById: (id) => apiService.get(`Rpt_builder2/html/build_report2/${id}`),

  getcolumnDetailsByurl: (url) => apiService.get(`Rpt_builder2_lines/geturlkeybyurl?url=${url}`),

  getAllDetailsByurl: (url) => apiService.get(`Rpt_builder2_lines/fetch_data_url?url=${url}`),

  downloadFile: async (format, dataList, name) => {
    const url = `${API_BASE_URL}/rbbuilder/fileconverter/downloadFile/${format}`;
    try {
      const fileData = await apiService.post(url, dataList, { responseType: 'blob' });
      const blob = new Blob([fileData], { type: 'application/octet-stream' });
      const fileName = name ? `${name}.${format}` : `download.${format}`;
      const link = document.createElement('a');
      link.href = window.URL.createObjectURL(blob);
      link.download = fileName;
      link.click();
    } catch (error) {
      console.error('Error downloading file:', error);
    }
  },
};

export default ReportBuilderService;
