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

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

// Configure multer for asset image upload
// const assetUpload = upload.fields([
//   { name: "asset_image", maxCount: 1 }
// ]);

// Alternative single file upload configuration
// const assetUpload = upload.single("asset_image");

/**
 * @swagger
 * tags:
 *   name: Assets
 *   description: Asset management - create, list, update, delete, assign assets APIs
 */

/* ===========================
   ✅ CREATE ASSET
=========================== */
router.post(
  "/create",
  authenticate,
//   assetUpload,
upload.single("image"),
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Create a new asset'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['asset_image'] = {
        in: 'formData',
        type: 'file',
        description: 'Asset image file',
        required: false
      }
      #swagger.parameters['asset_name'] = {
        in: 'formData',
        type: 'string',
        description: 'Name of the asset',
        required: true
      }
      #swagger.parameters['s_no'] = {
        in: 'formData',
        type: 'string',
        description: 'Serial number (auto-generated if not provided)',
        required: false
      }
      #swagger.parameters['employee_id'] = {
        in: 'formData',
        type: 'string',
        description: 'Employee ID to assign asset to',
        required: false
      }
      #swagger.parameters['status'] = {
        in: 'formData',
        type: 'string',
        description: 'Asset status (Available, Assigned, Under Maintenance, Retired)',
        required: false
      }
      #swagger.parameters['org_id'] = {
        in: 'formData',
        type: 'string',
        description: 'Organization ID',
        required: true
      }
    */
    assetsController.createAsset(req, res);
  }
);

/* ===========================
   ✅ GET ALL ASSETS (with pagination & filters)
=========================== */
router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Get all assets with pagination and filters'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = {
        in: 'query',
        type: 'integer',
        description: 'Page number',
        default: 1,
        required: false
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        type: 'integer',
        description: 'Records per page',
        default: 10,
        required: false
      }
      #swagger.parameters['status'] = {
        in: 'query',
        type: 'string',
        description: 'Filter by status',
        required: false
      }
      #swagger.parameters['org_id'] = {
        in: 'query',
        type: 'string',
        description: 'Filter by organization ID',
        required: false
      }
      #swagger.parameters['search'] = {
        in: 'query',
        type: 'string',
        description: 'Search by asset name, serial number, or employee name',
        required: false
      }
    */
    assetsController.getAllAssets(req, res);
  }
);

/* ===========================
   ✅ GET AVAILABLE ASSETS (for dropdown)
=========================== */
router.get(
  "/available",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Get all available assets (not assigned to anyone)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['org_id'] = {
        in: 'query',
        type: 'string',
        description: 'Filter by organization ID',
        required: false
      }
    */
    assetsController.getAvailableAssets(req, res);
  }
);

/* ===========================
   ✅ GET ASSETS BY EMPLOYEE
=========================== */
router.get(
  "/employee/:employee_id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Get all assets assigned to a specific employee'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['employee_id'] = {
        in: 'path',
        type: 'string',
        description: 'Employee ID',
        required: true
      }
    */
    assetsController.getAssetsByEmployee(req, res);
  }
);

/* ===========================
   ✅ GET ASSET BY ID
=========================== */
router.get(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Get asset by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Asset ID',
        required: true
      }
    */
    assetsController.getAssetById(req, res);
  }
);

/* ===========================
   ✅ UPDATE ASSET
=========================== */
router.put(
  "/:id",
  authenticate,
//   assetUpload,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Update asset by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Asset ID',
        required: true
      }
      #swagger.parameters['asset_image'] = {
        in: 'formData',
        type: 'file',
        description: 'Asset image file',
        required: false
      }
      #swagger.parameters['asset_name'] = {
        in: 'formData',
        type: 'string',
        description: 'Name of the asset',
        required: false
      }
      #swagger.parameters['s_no'] = {
        in: 'formData',
        type: 'string',
        description: 'Serial number',
        required: false
      }
      #swagger.parameters['employee_id'] = {
        in: 'formData',
        type: 'string',
        description: 'Employee ID to assign/unassign',
        required: false
      }
      #swagger.parameters['status'] = {
        in: 'formData',
        type: 'string',
        description: 'Asset status',
        required: false
      }
    */
    assetsController.updateAsset(req, res);
  }
);

/* ===========================
   ✅ ASSIGN ASSET TO EMPLOYEE
=========================== */
router.patch(
  "/:id/assign",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Assign asset to an employee'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Asset ID',
        required: true
      }
      #swagger.parameters['employee_id'] = {
        in: 'body',
        type: 'string',
        description: 'Employee ID to assign',
        required: true
      }
    */
    assetsController.assignAssetToEmployee(req, res);
  }
);

/* ===========================
   ✅ UPDATE ASSET STATUS
=========================== */
router.patch(
  "/:id/status",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Update asset status'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Asset ID',
        required: true
      }
      #swagger.parameters['status'] = {
        in: 'body',
        type: 'string',
        description: 'Asset status (Available, Assigned, Under Maintenance, Retired)',
        required: true
      }
      #swagger.parameters['remarks'] = {
        in: 'body',
        type: 'string',
        description: 'Remarks about status change',
        required: false
      }
    */
    assetsController.updateAssetStatus(req, res);
  }
);

/* ===========================
   ✅ DELETE ASSET (SOFT DELETE)
=========================== */
router.delete(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Assets']
      #swagger.summary = 'Soft delete asset by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Asset ID',
        required: true
      }
    */
    assetsController.deleteAsset(req, res);
  }
);

module.exports = router;