const express = require('express');
const VortaCommerceService = require('../services/vortaCommerce');
const wrapAsync = require('../helpers').wrapAsync;

const router = express.Router();

router.get('/state', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.getState(req.currentUser);
  res.status(200).send(payload);
}));

router.post('/orders/demo', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.createDemoOrder(req.currentUser);
  res.status(200).send(payload);
}));

router.post('/inventory/restock', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.restockLowStock(req.currentUser);
  res.status(200).send(payload);
}));

router.post('/payments/:paymentReference/capture', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.capturePayment(req.currentUser, req.params.paymentReference);
  res.status(200).send(payload);
}));

router.post('/orders/:orderNumber/advance', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.advanceOrder(req.currentUser, req.params.orderNumber);
  res.status(200).send(payload);
}));

router.post('/shipments/:trackingNumber/complete', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.completeShipment(req.currentUser, req.params.trackingNumber);
  res.status(200).send(payload);
}));

router.post('/campaigns/:campaignId/launch', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.launchCampaign(req.currentUser, req.params.campaignId);
  res.status(200).send(payload);
}));

router.post('/reports/close', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.closeDailyReport(req.currentUser);
  res.status(200).send(payload);
}));

router.patch('/settings', wrapAsync(async (req, res) => {
  const payload = await VortaCommerceService.updateSettings(req.currentUser, req.body.data || {});
  res.status(200).send(payload);
}));

module.exports = router;
