const express = require("express");
const router = express.Router();
const jwtAuth = require("../utils/jwtAuth");
const multer = require("multer");

// Set up Multer storage configuration
const storage = multer.memoryStorage();
const upload = multer({ storage });

// Import Controllers
const authController = require("../controllers/auth");
const userController = require("../controllers/user");
const assetController = require("../controllers/asset");
const productController = require("../controllers/product");
const orderController = require("../controllers/order");
const settingController = require("../controllers/setting");
const areaController = require("../controllers/area");
const landmarkController = require("../controllers/landmark");
const customerController = require("../controllers/customer");
const areaAllotmentController = require("../controllers/area-allotment");
const itemCategoriesController = require("../controllers/item-category");
const designationController = require("../controllers/designation");
const orderDetailsController = require("../controllers/order-details");
const orderReturnController = require("../controllers/order-return");
const orderReturnDetailsController = require("../controllers/order-return-details");
const collectionController = require("../controllers/collection");
const expensesController = require("../controllers/expenses");
const outstandingController = require("../controllers/outstanding");
const userRightController = require("../controllers/user-rights");

// Authentication routes
router.route("/auth/login").post(authController.login);

// User routes
router.route("/user")
  .post(jwtAuth(), upload.single('image'), userController.create)
  .get(jwtAuth(), userController.getAll);

router.route("/user/:user_id")
  .get(jwtAuth(), userController.getById)
  .put(jwtAuth(), upload.single('image'), userController.updateById)
  .delete(jwtAuth(), userController.deleteById);

router.route("/user/status/:user_id")
  .put(jwtAuth(), userController.statusById);

router.route("/user/password/:user_id")
  .post(jwtAuth(), userController.changePassword);

// Asset routes 
router
  .route("/asset")
  .post(jwtAuth(), upload.array('asset', 5), assetController.create)
  .get(jwtAuth(), assetController.getAll);

router
  .route("/asset/:asset_id")
  .get(jwtAuth(), assetController.getById)
  .put(jwtAuth(), upload.single('asset'), assetController.updateById)
  .delete(jwtAuth(), assetController.deleteById);

// Product routes
router.route("/product")
  .post(jwtAuth(), productController.create)
  .get(jwtAuth(), productController.getAll);

router.route("/product/:product_id")
  .get(jwtAuth(), productController.getById)
  .put(jwtAuth(), productController.updateById)
  .delete(jwtAuth(), productController.deleteById);

router.route("/bulk_import/product")
  .post(jwtAuth(), upload.single('file'), productController.bulkUpload);

// Order routes
router.route("/order")
  .post(jwtAuth(), orderController.create)
  .get(jwtAuth(), orderController.getAll);

router.route("/order/:order_id")
  .get(jwtAuth(), orderController.getById)
  .put(jwtAuth(), orderController.updateById)
  .delete(jwtAuth(), orderController.deleteById);

router.route("/order_no")
  .get(jwtAuth(), orderController.getOrderId);

router.route("/orderDetail/:order_detail_id")
  .delete(jwtAuth(), orderController.deleteOrderDetailById);

router.route("/bulk_import/order")
  .post(jwtAuth(), upload.single('file'), orderController.bulkUpload);

router.route("/draft/order")
  .get(jwtAuth(), orderController.getAllDeleteData);

router.route("/draft/restore/:order_id")
  .put(jwtAuth(), orderController.restoreById);

// Order Details routes
router.route("/bulk_import/order_details")
  .post(jwtAuth(), upload.single('file'), orderDetailsController.bulkUpload);

// Order Return routes
router.route("/order_return")
  .post(jwtAuth(), orderReturnController.create)
  .get(jwtAuth(), orderReturnController.getAll);

router.route("/order_return/:return_id")
  .get(jwtAuth(), orderReturnController.getById)
  .delete(jwtAuth(), orderReturnController.deleteById);

router.route("/bulk_import/order_return")
  .post(jwtAuth(), upload.single('file'), orderReturnController.bulkUpload);

// Order Return Details routes
router.route("/bulk_import/order_return_details")
  .post(jwtAuth(), upload.single('file'), orderReturnDetailsController.bulkUpload)

// Settings routes
router
  .route("/settings")
  .post(jwtAuth(), settingController.create)
  .get(settingController.getAll);

router
  .route("/settings/:setting_id")
  .get(settingController.getById)
  .put(jwtAuth(), settingController.updateById)
  .delete(jwtAuth(), settingController.deleteById);

// Contact Us routes
router
  .route("/contact_us")
  .post(jwtAuth(), upload.single('image'), settingController.createContactUs)
  .get(jwtAuth(), settingController.getContactUs);

router
  .route("/contact_us/:contact_us_id")
  .get(settingController.getById)
  .put(jwtAuth(), upload.single('image'), settingController.updateContactUs)

// Area routes
router.route("/area")
  .post(jwtAuth(), areaController.create)
  .get(jwtAuth(), areaController.getAll);

router.route("/area/:area_id")
  .get(jwtAuth(), areaController.getById)
  .put(jwtAuth(), areaController.updateById)
  .delete(jwtAuth(), areaController.deleteById);

router.route("/bulk_import/area")
  .post(jwtAuth(), upload.single('file'), areaController.bulkUpload);

// Landmark routes
router.route("/landmark")
  .post(jwtAuth(), landmarkController.create)
  .get(jwtAuth(), landmarkController.getAll);

router.route("/landmark/:landmark_id")
  .get(jwtAuth(), landmarkController.getById)
  .put(jwtAuth(), landmarkController.updateById)
  .delete(jwtAuth(), landmarkController.deleteById);

router.route("/bulk_import/landmark")
  .post(jwtAuth(), upload.single('file'), landmarkController.bulkUpload);

// Customer routes
router.route("/customer")
  .post(jwtAuth(), customerController.create)
  .get(jwtAuth(), customerController.getAll);

router.route("/customer/:customer_id")
  .get(jwtAuth(), customerController.getById)
  .put(jwtAuth(), customerController.updateById)
  .delete(jwtAuth(), customerController.deleteById);

router.route("/bulk_import/customer")
  .post(jwtAuth(), upload.single('file'), customerController.bulkUpload);

// Area Allotment routes
router.route("/area_allotment")
  .post(jwtAuth(), areaAllotmentController.create)
  .get(jwtAuth(), areaAllotmentController.getAll);

router.route("/area_allotment/:allotment_id")
  .get(jwtAuth(), areaAllotmentController.getById)
  .put(jwtAuth(), areaAllotmentController.updateById)
  .delete(jwtAuth(), areaAllotmentController.deleteById);

router.route("/bulk_import/area_allotment")
  .post(jwtAuth(), upload.single('file'), areaAllotmentController.bulkUpload);

// Item Category routes
router.route("/item_category")
  .post(jwtAuth(), itemCategoriesController.create)
  .get(jwtAuth(), itemCategoriesController.getAll);

router.route("/item_category/:item_category_id")
  .get(jwtAuth(), itemCategoriesController.getById)
  .put(jwtAuth(), itemCategoriesController.updateById)
  .delete(jwtAuth(), itemCategoriesController.deleteById);

router.route("/bulk_import/item_category")
  .post(jwtAuth(), upload.single('file'), itemCategoriesController.bulkUpload);

// Designation routes
router.route("/designation")
  .post(jwtAuth(), designationController.create)
  .get(jwtAuth(), designationController.getAll);

router.route("/designation/:designation_id")
  .get(jwtAuth(), designationController.getById)
  .put(jwtAuth(), designationController.updateById)
  .delete(jwtAuth(), designationController.deleteById);

router.route("/bulk_import/designation")
  .post(jwtAuth(), upload.single('file'), designationController.bulkUpload);

// Collection routes
router.route("/collection")
  .post(jwtAuth(), collectionController.create)
  .get(jwtAuth(), collectionController.getAll);

router.route("/collection/:collection_id")
  .get(jwtAuth(), collectionController.getById)
  .put(jwtAuth(), collectionController.updateById)
  .delete(jwtAuth(), collectionController.deleteById);

router.route("/bulk_import/collection")
  .post(jwtAuth(), upload.single('file'), collectionController.bulkUpload);

router.route("/report/collection")
  .get(jwtAuth(), collectionController.getCollectionReport);

router.route("/report/collection/user")
  .get(jwtAuth(), collectionController.getCollectionReportUser);

router.route("/report/collection/area")
  .get(jwtAuth(), collectionController.getCollectionReportArea);

// Expenses routes
router.route("/expenses")
  .post(jwtAuth(), expensesController.create)
  .get(jwtAuth(), expensesController.getAll);

router.route("/expenses/:expenses_id")
  .get(jwtAuth(), expensesController.getById)
  .put(jwtAuth(), expensesController.updateById)
  .delete(jwtAuth(), expensesController.deleteById);

// Outstanding routes
router.route("/outstanding")
  .get(jwtAuth(), outstandingController.getAll);

// User Right routes
router.route("/user-right")
  .post(jwtAuth(), userRightController.create);

router.route("/user-right/:user_id")
  .get(jwtAuth(), userRightController.getById)

module.exports = router;
