/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-require-imports */
const router = require('express').Router();

const STUBS = require('./admin').STUBS;

const commonError = { success: false, message: 'Что-то пошло не так' };

const sleep =
  (duration = 1000) =>
    (req, res, next) =>
      setTimeout(next, duration);

router.use(sleep());

router.get('/arm/masters', (req, res) => {
  res
    .status(/error/.test(STUBS.masters) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.masters)
        ? commonError
        : require(`../json/arm-masters/${STUBS.masters}.json`),
    );
});

router.post('/arm/masters', (req, res) => {
  res
    .status(/error/.test(STUBS.masters) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.masters)
        ? commonError
        : require(`../json/arm-masters/${STUBS.masters}.json`),
    );
});

router.patch('/arm/masters/:id', (req, res) => {
  res
    .status(/error/.test(STUBS.masters) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.masters)
        ? commonError
        : require(`../json/arm-masters/${STUBS.masters}.json`),
    );
});

router.delete('/arm/masters/:id', (req, res) => {
  res
    .status(/error/.test(STUBS.masters) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.masters)
        ? commonError
        : require(`../json/arm-masters/${STUBS.masters}.json`),
    );
});

router.patch('/orders/:id', (req, res) => {
  res
    .status(/error/.test(STUBS.orders) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.orders)
        ? commonError
        : require(`../json/arm-orders/${STUBS.orders}.json`),
    );
});

router.post('/arm/orders', (req, res) => {
  res
    .status(/error/.test(STUBS.orders) ? 500 : 200)
    .send(
      /^error$/.test(STUBS.orders)
        ? commonError
        : require(`../json/arm-orders/${STUBS.orders}.json`),
    );
});

router.get('/order/:orderId', ({ params }, res) => {
  const { orderId } = params;
  const stubName = `${orderId}-${STUBS.orderView}`;

  res
    .status(/error/.test(stubName) ? 500 : 200)
    .send(
      /^error$/.test(stubName)
        ? commonError
        : require(`../json/landing-order-view/${stubName}.json`),
    );
});

router.post('/order/create', (req, res) => {
  res.status(200).send({ success: true, body: { ok: true } });
});

router.use('/admin', require('./admin'));

module.exports = router;