// routes/userRoutes.js
const express = require("express");
const router = express.Router();
const userController = require("../controllers/userController");
const upload = require("../middlewares/upload");
const authenticate = require("../middlewares/authenticate");
const authorize = require("../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Users
 *   description: User management APIs
 */

// Register User (public)
router.post("/register", upload.single("profileImage"), (req, res) => {
  // #swagger.tags = ['Auth']
  // #swagger.summary = 'Register a new user'
  userController.registerUser(req, res);
});

// Login User (public)
router.post("/login", (req, res) => {
  /* 
  #swagger.tags = ['Auth']
  #swagger.summary = 'Login a user'
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: { email: "dev@servicenxt.in", password: "123456" }
    }
  */
  userController.loginUser(req, res);
});

// DEDICATED PASSWORD UPDATE ENDPOINT (For Admin/HR only)
router.put("/:id/password", authenticate, authorize(["admin", "superadmin", "hr"]), (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Update user password only (Admin/HR only)'
  // #swagger.security = [{ "bearerAuth": [] }]
  /* #swagger.parameters['body'] = {
    in: 'body',
    required: true,
    schema: {
      new_password: "newSecurePass123",
      confirm_password: "newSecurePass123"
    }
  }
  */

  userController.updateUserPassword(req, res);
});

// BULK PASSWORD UPDATE ENDPOINT (For Admin/HR only)
router.post("/passwords/bulk", authenticate, authorize(["admin", "superadmin", "hr"]), (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Bulk update passwords for multiple users (Admin/HR only)'
  // #swagger.security = [{ "bearerAuth": [] }]
  /* #swagger.parameters['body'] = {
    in: 'body',
    required: true,
    schema: {
      users: [
        { userId: 1, new_password: "pass123" },
        { userId: 2, new_password: "pass456" }
      ]
    }
  }
  */

  userController.bulkUpdateUserPasswords(req, res);
});

// delete User (admin only)
router.delete(
  "/delete/:id",
  authenticate,
  authorize(["admin", "superadmin"]),
  (req, res) => {
    // #swagger.tags = ['Users']
    // #swagger.summary = 'Delete a user'
    // #swagger.security = [{ "bearerAuth": [] }]

    userController.deleteUser(req, res);
  },
);

// GET /auth/validate
router.get("/validate", (req, res) => {
  /* 
  #swagger.tags = ['Auth']
  #swagger.summary = 'Validate user token'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  userController.validateToken(req, res);
});

// Forgot Password (public)
router.post("/forgot-password", (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Request password reset'
  userController.forgotPassword(req, res);
});

// Get All Users (admin/manager only)
router.get("/", authenticate, (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Get all users'
  // #swagger.security = [{ "bearerAuth": [] }]

  userController.getAllUsers(req, res);
});

// Get User by ID (self or admin/manager)
router.get("/:id", authenticate, (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Get user by ID'
  // #swagger.security = [{ "bearerAuth": [] }]

  userController.getUserById(req, res);
});

// Update User (self or admin)
router.put("/:id", authenticate, 
  // authorize(["admin"]), 
  (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Update user details'
  // #swagger.security = [{ "bearerAuth": [] }]

  userController.updateUser(req, res);
});

// Deactivate User (admin only)
// router.put(
//   "/deactivate/:id",
//   authenticate,
//   authorize(["admin"]),
//   (req, res) => {
//     // #swagger.tags = ['Users']
//     // #swagger.summary = 'Deactivate a user'
//     // #swagger.security = [{ "bearerAuth": [] }]

//     userController.deactivateUser(req, res);
//   },
// );

// Upload Profile Image (self or admin)
// router.put(
//   "/upload-profile/:id",
//   authenticate,
//   upload.single("profileImage"),
//   (req, res) => {
//     // #swagger.tags = ['Users']
//     // #swagger.summary = 'Upload profile image for user'
//     // #swagger.security = [{ "bearerAuth": [] }]

//     userController.profileImageUpload(req, res);
//   },
// );

// Logout from Device (authenticated user only)
router.post("/logout-device", authenticate, (req, res) => {
  // #swagger.tags = ['Users']
  // #swagger.summary = 'Logout user from a specific device'
  // #swagger.security = [{ "bearerAuth": [] }]

  userController.logoutFromDevice(req, res);
});

// router.put(
//   "/:id/status",
//   authenticate,
//   authorize(["admin", "superadmin"]),
//   (req, res) => {
//     // #swagger.tags = ['Users']
//     // #swagger.summary = 'update user status from a specific device'
//     // #swagger.security = [{ "bearerAuth": [] }]
//     userController.updateUserStatus(req, res);
//   },
// );

// router.get("/agents", authenticate, authorize(["admin", "manager"]),  (req, res) => {
//   // #swagger.tags = ['Agents']
//   // #swagger.summary = 'Get all agents'
//   // #swagger.security = [{ "bearerAuth": [] }]

//   userController.getAgents(req, res);
// });

/**
 * -------------------------------
 * SEND OTP TO AGENT
 * -------------------------------
 */
// router.post("/send-otp", (req, res) => {
//   /* 
//     #swagger.tags = ['Agents']
//     #swagger.summary = 'Send OTP to agent phone number'
//     #swagger.parameters['body'] = {
//       phone: '9876543210'
//     }
//     #swagger.responses[200] = {
//       description: 'OTP sent successfully'
//     }
//   */
//   userController.sendAgentOtp(req, res);
// });

/**
 * -------------------------------
 * VERIFY OTP & LOGIN AGENT
 * -------------------------------
 */
// router.post("/verify-otp", (req, res) => {
//   /*
//     #swagger.tags = ['Agents']
//     #swagger.summary = 'Verify OTP and login agent'
//     #swagger.parameters['body'] = {
//       phone: '9876543210',
//       otp: '123456',
//       deviceId: 'android-device-id',
//       appVersion: '1.0.0'
//     }
//     #swagger.responses[200] = {
//       description: 'OTP verified and login successful'
//     }
//   */
//   userController.verifyAgentOtp(req, res);
// });

// router.post("/resend-otp", (req, res) => {
//   /*
//     #swagger.tags = ['Agents']
//     #swagger.summary = 'Resend OTP to agent phone number'
//     #swagger.parameters['body'] = {
//       phone: '9876543210'
//     }
//     #swagger.responses[200] = {
//       description: 'OTP resent successfully'
//     }
//   */
//   userController.resendAgentOtp(req, res);
// });

// router.get("/agent/profile", authenticate, authorize(["agent"]), (req, res) => {
//   /*
//       #swagger.tags = ['Mobile Agent Profile']
//       #swagger.summary = 'Get agent profile details'
//       #swagger.description = 'Fetch logged-in agent profile details including profile image'
//       #swagger.security = [{ "bearerAuth": [] }]

//       #swagger.responses[200] = {
//         description: 'Agent profile fetched successfully',
//         schema: {
//           success: true,
//           message: "Agent profile fetched successfully",
//           data: {
//             _id: "65f123abc456",
//             name: "Rahul Kumar",
//             firstName: "Rahul",
//             lastName: "Kumar",
//             phone: "9876543210",
//             email: "rahul@gmail.com",
//             gender: "male",
//             address: {
//               line1: "LB Nagar",
//               city: "Hyderabad",
//               state: "Telangana",
//               pincode: "500074"
//             },
//             profileImage: "/uploads/images/profile_123.jpg",
//             updatedAt: "2026-01-21T09:42:12.456Z"
//           }
//         }
//       }

//       #swagger.responses[401] = {
//         description: 'Unauthorized or invalid token'
//       }

//       #swagger.responses[404] = {
//         description: 'Agent not found'
//       }
//     */
//   userController.getAgentProfile(req, res);
// });

// router.put("/agent/profile", authenticate, authorize(["agent"]), (req, res) => {
//   /*
//       #swagger.tags = ['Mobile Agent Profile']
//       #swagger.summary = 'Update agent profile details'
//       #swagger.description = 'Agent can update personal profile details (no image)'
//       #swagger.security = [{ "bearerAuth": [] }]

//       #swagger.parameters['body'] = {
//         in: 'body',
//         required: true,
//         schema: {
//           firstName: 'Vittal',
//           lastName: 'Rao',
//           email: 'agent@email.com',
//           phone: '9876543210',
//           gender: 'male',
//           address: {
//             street: 'LB Nagar',
//             city: 'Hyderabad',
//             state: 'Telangana',
//             country: 'India',
//             zip: '500070'
//           }
//         }
//       }

//       #swagger.responses[200] = {
//         description: 'Profile updated successfully'
//       }
//       #swagger.responses[404] = {
//         description: 'Agent not found'
//       }
//     */
//   userController.updateAgentProfileDetails(req, res);
// });

// router.put(
//   "/agent/profile/image",
//   authenticate,
//   authorize(["agent"]),
//   upload.single("profileImage"),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Mobile Agent Profile']
//       #swagger.summary = 'Update agent profile image'
//       #swagger.description = 'Agent can upload or update profile image'
//       #swagger.security = [{ "bearerAuth": [] }]

//       #swagger.consumes = ['multipart/form-data']

//       #swagger.parameters['profileImage'] = {
//         in: 'formData',
//         type: 'file',
//         required: true,
//         description: 'Agent profile image (jpg, png)'
//       }

//       #swagger.responses[200] = {
//         description: 'Profile image updated successfully'
//       }

//       #swagger.responses[400] = {
//         description: 'Profile image is required'
//       }

//       #swagger.responses[404] = {
//         description: 'Agent not found'
//       }
//     */
//     userController.updateAgentProfileImage(req, res);
//   },
// );

// router.put(
//   "/agent/change-password",
//   authenticate,
//   authorize(["agent", "admin"]),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Mobile Agent Profile']
//       #swagger.summary = 'Change password'
//       #swagger.description = 'User can change password using current password'
//       #swagger.security = [{ "bearerAuth": [] }]
//       #swagger.parameters['body'] = {
//         currentPassword: 'OldPassword@123',
//         newPassword: 'NewPassword@123',
//         confirmPassword: 'NewPassword@123'
//       }
//       #swagger.responses[200] = {
//         description: 'Password changed successfully'
//       }
//     */
//     userController.changePassword(req, res);
//   },
// );

// router.get(
//   "/agent/profile/history",
//   authenticate,
//   authorize(["agent"]),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Mobile Agent Profile']
//       #swagger.summary = 'Get agent profile with history'
//       #swagger.description = 'Fetch agent profile details and completed task history'
//       #swagger.security = [{ "bearerAuth": [] }]
//       #swagger.responses[200] = {
//         description: 'Agent profile fetched successfully'
//       }
//     */
//     userController.getAgentProfileWithHistory(req, res);
//   },
// );

// router.get(
//   "/",
//   authenticate,
//   authorize(["admin", "manager", "receptionist"]),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Users']
//       #swagger.summary = 'Get users (filter by role)'
//       #swagger.description = 'Fetch users with optional role-based filtering. Used to get doctors, staff, etc.'
//       #swagger.security = [{ "bearerAuth": [] }]
//       */
//     userController.getUsers(req, res);
//   },
// );

// router.get(
//   "/:id/profile",
//   authenticate,
//   authorize(["doctor", "admin", "manager", "receptionist"]),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Users']             
//       #swagger.summary = 'Get doctor profile'
//       #swagger.description = 'Fetch doctor profile details'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */
//     userController.getDoctorProfile(req, res);
//   },
// );

router.get("/auth/me", authenticate, (req, res) => {
  /*
       #swagger.tags = ['Auth']
  #swagger.summary = 'Get current user '
  #swagger.security = [{ "bearerAuth": [] }]
    */

  userController.getCurrentUser(req, res);
});

module.exports = router;
