


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

const customerController = require("../../controllers/sales/salesCustomerController");
const upload = require("../../middlewares/upload");
const authenticate = require("../../middlewares/authenticate");
// const authorize = require("../../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Sales Customer Master
 *   description: Customer creation, listing and deletion APIs
 */

router.post(
  "/create",
  authenticate,
  upload.fields([
    { name: "panFile", maxCount: 1 },
    { name: "licenseFile", maxCount: 1 },
    { name: "aadhaarFile", maxCount: 1 },
    { name: "incorporationFile", maxCount: 1 },
    { name: "chequeFile", maxCount: 1 },
  ]),
  (req, res) => {
    /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Create Customer'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']

      #swagger.parameters['gst'] = {
        in: 'formData',
        type: 'string',
        example: '27ABCDE1234F1Z5'
      }

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

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

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

      #swagger.parameters['email'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: 'test@gmail.com'
      }

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

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

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

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

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

    customerController.createCustomer(req, res);
  }
);

router.put(
  "/:id",
  authenticate,
  upload.fields([
    { name: "panFile", maxCount: 1 },
    { name: "licenseFile", maxCount: 1 },
    { name: "aadhaarFile", maxCount: 1 },
    { name: "incorporationFile", maxCount: 1 },
    { name: "chequeFile", maxCount: 1 },
  ]),
  (req, res) =>{
authenticate,
    // swagger comments

    /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Update Customer'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        example: 'C-17123456789'
      }
      #swagger.parameters['gst'] = {
        in: 'formData',
        type: 'string',
        example: '27ABCDE1234F1Z5'
      }
      #swagger.parameters['firstName'] = {
        in: 'formData',
        type: 'string',  
        required: true,
        example: 'Ramesh'
      }
      #swagger.parameters['lastName'] = {
        in: 'formData',
        type: 'string', 
        example: 'Kumar'
      }
      #swagger.parameters['phone'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: '9876543210'
      }
      #swagger.parameters['email'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: 'test@gmail.com'
      }
    */

    customerController.updateCustomer(req, res);

  }
);


router.get(
  "/download-template/",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Download customer bulk upload template'
      #swagger.security = [{ "bearerAuth": [] }]
    */

     console.log("is api hitting heresadyguhjkl")
    customerController.downloadCustomerTemplate(req, res);
  }
);



router.get(
  "/all",
  authenticate,
  // authorize(["admin"]),
  (req, res) => {
    
    /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Get all customers'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    customerController.getCustomers(req, res);
  }
);

router.get(
  "/:id",
   authenticate,
    (req, res) => {
    
    /**
 * @swagger
 * /customers/{id}:
 *   get:
 *     summary: Get customer by ID
 *     tags: [Customer Master]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         example: C-17123456789
 *     responses:
 *       200:
 *         description: Customer details
 *       404:
 *         description: Not found
 */

     customerController.getCustomerById(req, res);
  }
   
  );

router.delete(
  "/:id",
  authenticate,
  // authorize(["admin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Customer Master']
      #swagger.summary = 'Delete customer (soft delete)'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    customerController.deleteCustomer(req, res);
  }
);


router.post(
  "/bulk-upload",
  authenticate,
  upload.single('file'),
  (req, res) => {
    /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Bulk upload customers via Excel file'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['file'] = {
        in: 'formData',
        type: 'file',
        required: true,
        description: 'Excel file with customer data'
      }
    */

    customerController.bulkUploadCustomers(req, res);
  }
);

router.post("/check-gst",authenticate, (req,res)=>{
  /*
      #swagger.tags = ['Sales Customer Master']
      #swagger.summary = 'Check for duplicate GST number'   
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["gst"],
              properties: {
                gst: {  type: "string", example: "27ABCDE1234F1Z5" }
              }
            }
          }
        }
      }
    */

    customerController.checkGstDuplicate(req,res);
});



module.exports = router;