import express from "express";
import { verifyToken, authorizeModules } from "../middlewares/authJwt";
import {
  createGroup,
  deleteGroup,
  getGroup,
  listGroups,
  updateGroup,
} from "../controllers/groupController";

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Groups
 *   description: Users groups management
 */

/**
 * @swagger
 * components:
 *   securitySchemes:
 *     bearerAuth:
 *       type: http
 *       scheme: bearer
 *       bearerFormat: JWT
 *   schemas:
 *     UsersGroup:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *         title:
 *           type: string
 *         description:
 *           type: string
 *           nullable: true
 *         users:
 *           type: array
 *           items:
 *             type: string
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 *       example:
 *         _id: "665e3e2f2a9bd8c4b2c12345"
 *         title: "VIP Clients"
 *         description: "Top tier clients"
 *         users: ["665e3e2f2a9bd8c4b2c11111", "665e3e2f2a9bd8c4b2c22222"]
 *         createdAt: "2025-09-25T16:00:00.000Z"
 *         updatedAt: "2025-09-25T16:00:00.000Z"
 *     CreateGroupRequest:
 *       type: object
 *       required: [title]
 *       properties:
 *         title:
 *           type: string
 *         description:
 *           type: string
 *         users:
 *           type: array
 *           items:
 *             type: string
 *       example:
 *         title: "VIP Clients"
 *         description: "Top tier clients"
 *         users: ["665e3e2f2a9bd8c4b2c11111", "665e3e2f2a9bd8c4b2c22222"]
 *     UpdateGroupRequest:
 *       allOf:
 *         - $ref: '#/components/schemas/CreateGroupRequest'
 */

/**
 * @swagger
 * /groups:
 *   post:
 *     operationId: createGroup
 *     tags: [Groups]
 *     summary: Create a users group
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreateGroupRequest'
 *     responses:
 *       201:
 *         description: Grupo creado correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Group created"
 *                 data:
 *                   $ref: '#/components/schemas/UsersGroup'
 *             example:
 *               success: true
 *               message: "Group created"
 *               data:
 *                 _id: "665e3e2f2a9bd8c4b2c12345"
 *                 title: "VIP Clients"
 *                 description: "Top tier clients"
 *                 users: ["665e3e2f2a9bd8c4b2c11111", "665e3e2f2a9bd8c4b2c22222"]
 *                 createdAt: "2025-09-25T16:00:00.000Z"
 *                 updatedAt: "2025-09-25T16:00:00.000Z"
 *       400:
 *         description: Payload inválido (validación Joi)
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *             example:
 *               success: false
 *               message: "\"title\" is required"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 *   get:
 *     operationId: listGroups
 *     tags: [Groups]
 *     summary: List groups
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *       - in: query
 *         name: size
 *         schema:
 *           type: integer
 *           default: 10
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Lista de grupos obtenida correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Groups retrieved"
 *                 data:
 *                   type: object
 *                   properties:
 *                     data:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/UsersGroup'
 *                     pageInfo:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           totalRecords:
 *                             type: number
 *             example:
 *               success: true
 *               message: "Groups retrieved"
 *               data:
 *                 data:
 *                   - _id: "665e3e2f2a9bd8c4b2c12345"
 *                     title: "VIP Clients"
 *                     description: "Top tier clients"
 *                     users: ["665e3e2f2a9bd8c4b2c11111"]
 *                     createdAt: "2025-09-25T16:00:00.000Z"
 *                     updatedAt: "2025-09-25T16:00:00.000Z"
 *                 pageInfo:
 *                   - totalRecords: 1
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */

router.use(verifyToken, authorizeModules("notifications"));

/**
 * Routes: Create/List groups
 */
router.route("/").post(createGroup).get(listGroups);
/**
 * @swagger
 * /groups/{id}:
 *   get:
 *     operationId: getGroup
 *     tags: [Groups]
 *     summary: Get a group by id
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Grupo obtenido correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Group retrieved"
 *                 data:
 *                   $ref: '#/components/schemas/UsersGroup'
 *             example:
 *               success: true
 *               message: "Group retrieved"
 *               data:
 *                 _id: "665e3e2f2a9bd8c4b2c12345"
 *                 title: "VIP Clients"
 *                 description: "Top tier clients"
 *                 users: ["665e3e2f2a9bd8c4b2c11111", "665e3e2f2a9bd8c4b2c22222"]
 *                 createdAt: "2025-09-25T16:00:00.000Z"
 *                 updatedAt: "2025-09-25T16:00:00.000Z"
 *       400:
 *         description: ID inválido
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Invalid id"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 *       404:
 *         description: Grupo no encontrado
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Group not found"
 *   patch:
 *     operationId: updateGroup
 *     tags: [Groups]
 *     summary: Update a group
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/UpdateGroupRequest'
 *     responses:
 *       200:
 *         description: Grupo actualizado correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Group updated"
 *                 data:
 *                   $ref: '#/components/schemas/UsersGroup'
 *             example:
 *               success: true
 *               message: "Group updated"
 *               data:
 *                 _id: "665e3e2f2a9bd8c4b2c12345"
 *                 title: "VIP Clients Updated"
 *                 description: "Updated description"
 *                 users: ["665e3e2f2a9bd8c4b2c11111"]
 *                 createdAt: "2025-09-25T16:00:00.000Z"
 *                 updatedAt: "2025-09-26T10:00:00.000Z"
 *       400:
 *         description: ID inválido o payload inválido
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *             examples:
 *               invalid_id:
 *                 summary: ID inválido
 *                 value:
 *                   success: false
 *                   message: "Invalid id"
 *               validation:
 *                 summary: Error de validación
 *                 value:
 *                   success: false
 *                   message: "\"title\" is required"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 *       404:
 *         description: Grupo no encontrado
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Group not found"
 *   delete:
 *     operationId: deleteGroup
 *     tags: [Groups]
 *     summary: Delete a group
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Grupo eliminado correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Group deleted"
 *             example:
 *               success: true
 *               message: "Group deleted"
 *       400:
 *         description: ID inválido
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Invalid id"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 *       404:
 *         description: Grupo no encontrado
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Group not found"
 */
router.route("/:id").get(getGroup).patch(updateGroup).delete(deleteGroup);

export default router;
