const express = require("express");
const router = express.Router();
const vehicleController = require("../../controllers/postgre/vehicleController");
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");
const upload = require("../../middlewares/upload");

/**
 * @swagger
 * tags:
 *   name: Commercial Vehicles Postgre
 *   description: Commercial vehicle entry, approval and exit APIs
 */

router.post(
  "/create",
  // upload.fields([
  //   { name: "invoiceFiles" },
  //   { name: "idProofFiles" },
  //   { name: "deliveryFiles" },
  //   { name: "materialFiles" },
  // ]),
  authenticate,
  //   authorize(["admin", "qc", "security"]),
  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles Postgre' ]
      #swagger.summary = 'Create commercial vehicle gate entry'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']

      #swagger.parameters['vehicleNumber'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: 'TS09AB1234'
      }

      #swagger.parameters['driverName'] = {
        in: 'formData',
        type: 'string',
        example: 'Ramesh Kumar'
      }

      #swagger.parameters['vendorName'] = {
        in: 'formData',
        type: 'string',
        example: 'ABC Logistics'
      }

      #swagger.parameters['invoiceFiles'] = {
        in: 'formData',
        type: 'file'
      }

      #swagger.parameters['idProofFiles'] = {
        in: 'formData',
        type: 'file'
      }

      #swagger.parameters['deliveryFiles'] = {
        in: 'formData',
        type: 'file'
      }

      #swagger.parameters['materialFiles'] = {
        in: 'formData',
        type: 'file'
      }

    */

    vehicleController.createVehicleEntry(req, res);
  },
);

router.post(
  "/blacklist/create",
  authenticate,
  authorize(["security", "qc", "admin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Blacklist']
      #swagger.summary = 'Create a blacklisted vehicle entry'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              properties: {
                vehicleNumber: { type: "string", example: "TS09AB1234" },
                reason: { type: "string", example: "Unauthorized entry attempt" }
              }
            }
          }
        }
      }
    */
    vehicleController.createBlacklistVehicle(req, res);
  },
);

router.get(
  "/all",
  authenticate,
  //   authorize(["admin", "qc", "security"]),
  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Get all commercial vehicle entries'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    vehicleController.getAllVehicles(req, res);
  },
);

router.post(
  "/blacklist",
  authenticate,
  // authorize(["admin", "security"]),
  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Blacklist commercial vehicle based on Id'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vehicleController.blacklistVehicle(req, res);
  },
);

router.get("/weighbridge/cards", authenticate, (req, res) => {
  /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Get commercial vehicle weightbridgecards'
      #swagger.security = [{ "bearerAuth": [] }]
    */
  vehicleController.getWeighbridgeCards(req, res);
});

router.get(
  "/prefills/:vehicleNumber",
  authenticate,
  // authorize(["admin", "qc", "security"]),
  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Get prefill commercial vehicle details'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    vehicleController.getVehiclePrefill(req, res);
  },
);

router.get(
  "/blacklist/all",
  authenticate,
  authorize(["security", "qc", "admin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Get all blacklisted vehicles'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vehicleController.getBlacklistVehicles(req, res);
  },
);

router.get("/check/:plateNumber", (req, res) => {
  /**
   * #swagger.tags = ['Commercial Vehicles Postgre']
   * #swagger.summary = 'Check if vehicle is blacklisted'
   * #swagger.description = 'Returns whether the given plate number is blocked'
   */
  vehicleController.checkVehicleBlacklist(req, res);
});

router.put(
  "/unblock/:id",
  authenticate,
  authorize(["admin", "security"]),
  (req, res) => {
    // #swagger.tags = ['Commercial Vehicles Postgre']
    // #swagger.summary = 'Unblock a blacklisted vehicle'
    // #swagger.description = 'Deactivates blacklist entry and removes blacklist from vehicles'
    // #swagger.parameters['id'] = { description: 'Blacklist record ID', required: true }

    vehicleController.unblockVehicle(req, res);
  },
);

router.put(
  "/update/:id",
  authenticate,
  upload.fields([
    { name: "invoiceFiles" },
    { name: "idProofFiles" },
    { name: "deliveryFiles" },
    { name: "materialFiles" },
  ]),

  (req, res) => {
    /*
      #swagger.tags = ['Commercial Vehicles']
      #swagger.summary = 'Update vehicle entries by Id'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    vehicleController.updateVehicleEntry(req, res);
  },
);

router.put(
  "/exit/:id",
  authenticate,
  (req, res) => {
    /*
        #swagger.tags = ['Commercial Vehicles']
        #swagger.summary = 'Mark commercial vehicle exit from gate'
        #swagger.security = [{ "bearerAuth": [] }]
  
        #swagger.parameters['id'] = {
          in: 'path',
          required: true,
          type: 'string',
          example: '665f3f0c8f9b123456789abc'
        }
      */

    vehicleController.markVehicleExit(req, res);
  },
);

router.put(
  "/delete/:id",
  authenticate,
  (req, res) => {
    vehicleController.deleteVehicle(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Soft delete commercial vehicle'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.put(
  "/vehicleExit/:id",
  authenticate,
  (req, res) => {
    vehicleController.exitVehicle(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Mark vehicle exit / gate out'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.get(
  "/weighbridge/vehicle/:id",
  authenticate,
  (req, res) => {
    vehicleController.getWeighbridgeVehicleById(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Get weighbridge vehicle details by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.get(
  "/weighment-slip/:id",
  authenticate,
  (req, res) => {
    vehicleController.downloadWeighmentSlip(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Download weighment slip PDF'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.get(
  "/waiting-vehicles",
  authenticate,
  (req, res) => {
    vehicleController.getDispatchWaitingVehicles(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Get dispatch waiting vehicles'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.put(
  "/update-gate-in/:id",
  authenticate,
  (req, res) => {
    vehicleController.updateGateIn(req, res);
  },
  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Update shipment gate-in status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.post(
  "/create-dispatch",
  authenticate,
  (req, res) => {
    vehicleController.createDispatchVehicleEntry(req, res);
  },

  /*
    #swagger.tags = ['Commercial Vehicles Postgre']
    #swagger.summary = 'Create dispatch vehicle entry'
    #swagger.security = [{ "bearerAuth": [] }]
  */
);

router.get("/weighbridge-dispatch/:shipmentId", authenticate, (req, res) => {
  /*
  #swagger.tags = ['Commercial Vehicles Postgre']
  #swagger.summary = 'Get shipment dispatch details for weighbridge'
  #swagger.security = [{ "bearerAuth": [] }]
*/
  vehicleController.getDispatchWeighbridgeDetails(req, res);
});


router.get(
  "/material-group/:soNumber",
  authenticate,
  (req, res) => {
    
/*
  #swagger.tags = ['Dispatch']
  #swagger.summary = 'Get material group by sales order'
  #swagger.security = [{ "bearerAuth": [] }]
*/
    vehicleController.getMaterialGroupBySO(req, res);
  }
);


router.put(
  "/dispatch-weighment/:id",
  authenticate,
  (req, res) => {
    vehicleController.completeDispatchWeighment(req, res);
  }
);


//   router.put("/delete/:id", authenticate, (req, res)=>{
//       vehicleController.deleteVehicle(req, res);
//   });

//   router.put("/vehicleExit/:id", authenticate, (req,res)=>{
//     vehicleController.exitVehicle(req, res);
//   })

//   router.get("/weighbridge/vehicle/:id",authenticate, (req,res)=>{
//      vehicleController.getWeighbridgeVehicleById(req,res);
//   });

//   router.get("/weighment-slip/:id",authenticate, (req,res) =>{
//     vehicleController.downloadWeighmentSlip(req,res);
//   }

// );

// router.get("/waiting-vehicles",
//   authenticate,(req,res) =>{
//     vehicleController.getDispatchWaitingVehicles(req,res);
//   }

// );

// router.put("/update-gate-in/:id",
//   authenticate,(req,res) =>{
//     vehicleController.updateGateIn(req,res);
//   }

// );

// router.post("/create-dispatch",authenticate, (req,res)=>{
//   vehicleController.createDispatchVehicleEntry(req,res);
// }

// );

router.get("/dispatch/return-weighbridge/:id",
  authenticate,
  (req,res) =>{
    vehicleController.getReturnDispatchWeighbridgeDetails(req,res);

  }
);


router.put("/dispatch/weighbridge/exit/:id",
  authenticate,
  (req,res) =>{
    vehicleController.completeDispatchReturnWeight(req,res);
  }
);


router.get("/download-dispatch-document/:id",
  authenticate,
  (req,res) =>{
    vehicleController.downloadDispatchDocument(req,res);
  }
);


router.get("/po/vendors",
  authenticate,
  (req,res) =>{
    vehicleController.getPOVendors(req,res);
  }
);

router.get("/po/vendor-pos",
  authenticate,
  (req,res) =>{
    vehicleController.getPOsByVendor(req,res);
  }
);


router.get("/weighbridge/card", authenticate, (req, res) => {
  /*
      #swagger.tags = ['Commercial Vehicles Postgre']
      #swagger.summary = 'Get commercial vehicle weightbridgecards'
      #swagger.security = [{ "bearerAuth": [] }]
    */
  vehicleController.getWeighbridgeCard(req, res);
});


module.exports = router;
