/* 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('/order/: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) => {
  const stubName = STUBS.orderCreate;

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

router.post('/order/:orderId/upload-car-img', (req, res) => {
  const { orderId } = req.params;
  const stubName = `${orderId}-${STUBS.orderCarImg}`;

  try {
    res
      .status(/error/.test(stubName) ? 500 : 200)
      .send(require(`../json/landing-order-car-image-upload/${stubName}.json`));
  } catch (e) {
    console.error(e);
    res
      .status(500)
      .send(commonError);
  }
});

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

module.exports = router;
