import express, { Router } from 'express';

import * as customerValidation from '@/modules/customer/customer.validation';
import * as customerController from '@/modules/customer/customer.controller';

import * as paymentController from '@/modules/customer/payment/payment.controller';
import * as paymentValidation from '@/modules/customer/payment/payment.validation';

import { validateMiddleware } from '@/shared/utils/middlewares/index';
import userAuthAndCheckPermissions from '@/modules/auth/auth.user.middleware';
import { setAuditFields } from '@/shared/middleware/setAuditFields';
import { AuditMode } from '@/shared/constants/enum.constant';

const router: Router = express.Router();

router
  .route('/')
  .get(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.getCustomers),
    customerController.getCustomers,
  )
  .post(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.createCustomer()),
    setAuditFields({ mode: AuditMode.CREATE, companyField: 'company' }),
    customerController.createCustomer,
  );

router
  .route('/:id/payment')
  .patch(
    userAuthAndCheckPermissions(),
    validateMiddleware(paymentValidation.upsertCustomerPaymentTimeline()),
    setAuditFields({ mode: AuditMode.CREATE, companyField: 'company' }),
    paymentController.createPayment,
  )
  .get(userAuthAndCheckPermissions(), paymentController.getPaymentTimelines);

router
  .route('/:id/payment/reminder')
  .post(
    userAuthAndCheckPermissions(),
    validateMiddleware(paymentValidation.sendPaymentReminder()),
    paymentController.sendPaymentReminder,
  );

router
  .route('/:id/payment/stages/:stageId/entries')
  .post(
    userAuthAndCheckPermissions(),
    validateMiddleware(paymentValidation.addPaymentEntry()),
    paymentController.addPaymentEntry,
  );

router
  .route('/:id/payment/stages/:stageId/entries/:entryId')
  .patch(
    userAuthAndCheckPermissions(),
    validateMiddleware(paymentValidation.updatePaymentEntry()),
    paymentController.updatePaymentEntry,
  )
  .delete(
    userAuthAndCheckPermissions(),
    validateMiddleware(paymentValidation.deletePaymentEntry()),
    paymentController.deletePaymentEntry,
  );

router
  .route('/:id')
  .get(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.getCustomerById),
    customerController.getCustomerById,
  )
  .patch(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.updateCustomer()),
    setAuditFields({ mode: AuditMode.UPDATE, companyField: 'company' }),
    customerController.updateCustomer,
  )
  .delete(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.deleteCustomerById),
    customerController.deleteCustomerById,
  );

router
  .route('/:id/document-status')
  .patch(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.updateCustomerDocumentStatus),
    setAuditFields({ mode: AuditMode.UPDATE, companyField: 'company' }),
    customerController.updateCustomerDocumentStatus,
  );

router
  .route('/:id/cancel-booking')
  .post(
    userAuthAndCheckPermissions(),
    validateMiddleware(customerValidation.cancelCustomerBooking),
    customerController.cancelCustomerBooking,
  );

export default router;
