

const express = require("express");
const router = express.Router();

const assetsController = require("../../controllers/hrandpayrollsController/assetsController");
const authenticate = require("../../middlewares/authenticate");
const upload = require("../../middlewares/upload");

/**
 * @swagger
 * tags:
 *   name: Assets
 *   description: Asset management APIs
 */

/* ============================================================
   POST ROUTES
============================================================ */

router.post("/create", authenticate, upload.single("asset_image"), (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Create a new asset'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
  */
  assetsController.createAsset(req, res);
});

router.post("/bulk-assign", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Bulk assign multiple assets to one employee'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.bulkAssignAssets(req, res);
});

router.post("/bulk-return", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Bulk return multiple assets from an employee'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.bulkReturnAssets(req, res);
});



/* ============================================================
   GET ROUTES — STATIC (must all come before /:id)
============================================================ */

router.get("/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get all assets with pagination and filters'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getAllAssets(req, res);
});

router.get("/available", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get all available (unassigned) assets'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getAvailableAssets(req, res);
});

router.get("/employee-assets", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get all employees with their assigned assets'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getEmployeesWithAssets(req, res);
});

router.get("/return-stats", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get asset return statistics'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getReturnStats(req, res);
});

router.get("/return-logs", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get asset return history with pagination and filters'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getReturnLogs(req, res);
});

router.get("/employees/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get all active employees for asset assignment'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getAllEmployeesForAssign(req, res);
});

router.get("/employees/no-assets", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get employees with no assets assigned'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getEmployeesWithNoAssets(req, res);
});

/* ============================================================
   GET ROUTES — PARAMETERIZED (after all static GET routes)
============================================================ */

router.get("/employee/:employee_id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get all assets assigned to a specific employee'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getAssetsByEmployee(req, res);
});

router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Get asset by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.getAssetById(req, res);
});

/* ============================================================
   PUT / PATCH / DELETE ROUTES
============================================================ */

router.put("/:id", authenticate, upload.single("asset_image"), (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Update asset by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
  */
  assetsController.updateAsset(req, res);
});

router.patch("/:id/assign", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Assign asset to an employee'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.assignAssetToEmployee(req, res);
});

router.patch("/:id/return", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Return a single asset from an employee'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.returnAsset(req, res);
});

router.patch("/:id/status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Update asset status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.updateAssetStatus(req, res);
});

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Assets']
    #swagger.summary = 'Soft delete asset by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  assetsController.deleteAsset(req, res);
});

module.exports = router;