const express = require('express');

const IntegrationsService = require('../services/integrations');
const wrapAsync = require('../helpers').wrapAsync;
const { checkPermissions } = require('../middlewares/check-permissions');

const router = express.Router();

router.get(
  '/',
  checkPermissions('READ_API_DOCS'),
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.status();
    res.status(200).send(payload);
  }),
);

router.get(
  '/csv/products',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.exportProductsCsv(
      req.currentUser,
    );
    res.setHeader('Content-Type', 'text/csv; charset=utf-8');
    res.setHeader(
      'Content-Disposition',
      'attachment; filename="northstar-product-catalog.csv"',
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/google-sheets/export-products',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.exportProductsToGoogleSheets(
      req.currentUser,
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/stripe/checkout',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.createStripeCheckoutSession(
      req.body || {},
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/hubspot/sync-sample-request',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.syncSampleRequestToHubSpot(
      req.body || {},
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/quickbooks/create-invoice',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.createQuickBooksInvoice(
      req.body || {},
    );
    res.status(200).send(payload);
  }),
);

router.post(
  '/delivery/create-quote',
  wrapAsync(async (req, res) => {
    const payload = await IntegrationsService.createDeliveryWorkflow(
      req.body || {},
    );
    res.status(200).send(payload);
  }),
);

router.use('/', require('../helpers').commonErrorHandler);

module.exports = router;
