/* eslint-disable node/handle-callback-err */
const axios = require('axios')
const { verityAxiosConfig, panatechAxiosConfig } = require('../ArpaWebservice')
function axiosConfig(db) {
  if (db === 'verity') return verityAxiosConfig()
  else if (db === 'panatech') return panatechAxiosConfig()
  else return {}
}

/// //// cross server requests
module.exports.getRouteManager = [
  (req, res) => {
    const { url, db } = req.body
    axios
      .get(url, axiosConfig(db))
      .then(arpaRes => {
        if (arpaRes.data.error) return res.status(404).json(arpaRes.data.error)
        else return res.json(arpaRes.data)
      })
      .catch(err => {
        console.log('🚀 ~ file: arpaConnectionController.js ~ getRouteManager ~ err', err, "-------",url,db)
        return res.status(500).json({ message: 'WebService Error' })
      })
  }
]
module.exports.postRouteManager = [
  (req, res) => {
    const { url, db, data } = req.body
    axios
      .post(url, data, axiosConfig(db))
      .then(arpaRes => {
        if (arpaRes.data.error) return res.status(404).json(arpaRes.data.error)
        else return res.json(arpaRes.data)
      })
      .catch(err => {
        // console.log('🚀 ~ file: arpaConnectionController.js ~ postRouteManager ~ err', err)
        return res.status(500).json({ message: 'WebService Error' })
      })
  }
]
module.exports.putRouteManager = [
  (req, res) => {
    const { url, db, data } = req.body
    axios
      .put(url, data, axiosConfig(db))
      .then(arpaRes => {
        if (arpaRes.data.error) return res.status(404).json(arpaRes.data.error)
        else return res.json(arpaRes.data)
      })
      .catch(err => {
        // console.log('🚀 ~ file: arpaConnectionController.js ~ putRouteManager ~ err', err)
        return res.status(500).json({ message: 'WebService Error' })
      })
  }
]
module.exports.deleteRouteManager = [
  (req, res) => {
    const { url, db, data } = req.body
    axios
      .delete(url, data, axiosConfig(db))
      .then(arpaRes => {
        if (arpaRes.data.error) return res.status(404).json(arpaRes.data.error)
        else return res.json(arpaRes.data)
      })
      .catch(err => {
        // console.log('🚀 ~ file: arpaConnectionController.js ~ deleteRouteManager ~ err', err)
        return res.status(500).json({ message: 'WebService Error' })
      })
  }
]
