import express from "express";
import {
  deleteNotifications,
  getNotificationCounter,
  getNotifications,
  markAsRead,
  markAsUnRead,
  editConfiguration,
  getConfiguration,
} from "../controllers/notificationController";
import { verifyToken } from "../middlewares/authJwt";

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Notifications
 *   description: Notificaciones v1 - gestión de notificaciones y preferencias
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Notification:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *         user:
 *           type: string
 *         message:
 *           type: string
 *         type:
 *           type: string
 *           enum: [info, success, warning, error, danger]
 *         isRead:
 *           type: boolean
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 *     NotificationCounter:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *         user:
 *           type: string
 *         count:
 *           type: integer
 *     NotificationsConfig:
 *       type: object
 *       properties:
 *         email:
 *           type: boolean
 *         sms:
 *           type: boolean
 *         push:
 *           type: boolean
 *         promotions:
 *           type: boolean
 */

router.use(verifyToken);

/**
 * @swagger
 * /notifications/counter:
 *   get:
 *     operationId: getNotificationCounter
 *     tags: [Notifications]
 *     summary: Obtener contador de notificaciones no leídas
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Contador obtenido correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: integer
 *                   description: Cantidad de notificaciones no leídas
 *                   example: 5
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.get("/counter", getNotificationCounter);

/**
 * @swagger
 * /notifications/{page}/{size}:
 *   get:
 *     operationId: getNotifications
 *     tags: [Notifications]
 *     summary: Listar notificaciones paginadas
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: page
 *         required: true
 *         schema:
 *           type: integer
 *         description: Número de página (1-based)
 *       - in: path
 *         name: size
 *         required: true
 *         schema:
 *           type: integer
 *         description: Cantidad de elementos por página
 *     responses:
 *       200:
 *         description: Lista de notificaciones obtenida correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, notifications]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Notifications retrieved successfully"
 *                 notifications:
 *                   type: object
 *                   properties:
 *                     data:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Notification'
 *                     pageInfo:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           totalRecords:
 *                             type: integer
 *             example:
 *               success: true
 *               message: "Notifications retrieved successfully"
 *               notifications:
 *                 data:
 *                   - _id: "665e3e2f2a9bd8c4b2c12345"
 *                     message: "Pago recibido"
 *                     type: "success"
 *                     isRead: false
 *                     createdAt: "2025-03-09T10:00:00.000Z"
 *                     updatedAt: "2025-03-09T10:00:00.000Z"
 *                 pageInfo:
 *                   - totalRecords: 15
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.get("/:page/:size", getNotifications);

/**
 * @swagger
 * /notifications/mark-as-read:
 *   post:
 *     operationId: markNotificationsAsRead
 *     tags: [Notifications]
 *     summary: Marcar notificaciones como leídas
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [ids]
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: IDs de las notificaciones a marcar como leídas
 *           example:
 *             ids: ["665e3e2f2a9bd8c4b2c12345", "665e3e2f2a9bd8c4b2c12346"]
 *     responses:
 *       200:
 *         description: Notificaciones marcadas como leídas correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Notifications marked as read successfully"
 *                 data:
 *                   $ref: '#/components/schemas/NotificationCounter'
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.post("/mark-as-read", markAsRead);

/**
 * @swagger
 * /notifications/mark-as-unread:
 *   post:
 *     operationId: markNotificationsAsUnread
 *     tags: [Notifications]
 *     summary: Marcar notificaciones como no leídas
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [ids]
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: IDs de las notificaciones a marcar como no leídas
 *           example:
 *             ids: ["665e3e2f2a9bd8c4b2c12345"]
 *     responses:
 *       200:
 *         description: Notificaciones marcadas como no leídas correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Notifications marked as unread successfully"
 *                 data:
 *                   $ref: '#/components/schemas/NotificationCounter'
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.post("/mark-as-unread", markAsUnRead);

/**
 * @swagger
 * /notifications/delete:
 *   post:
 *     operationId: deleteNotifications
 *     tags: [Notifications]
 *     summary: Eliminar notificaciones
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [ids]
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: string
 *                 description: IDs de las notificaciones a eliminar
 *           example:
 *             ids: ["665e3e2f2a9bd8c4b2c12345", "665e3e2f2a9bd8c4b2c12346"]
 *     responses:
 *       200:
 *         description: Notificaciones eliminadas correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Notifications deleted successfully"
 *                 data:
 *                   $ref: '#/components/schemas/NotificationCounter'
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.post("/delete", deleteNotifications);

/**
 * @swagger
 * /notifications/configuration:
 *   get:
 *     operationId: getNotificationConfiguration
 *     tags: [Notifications]
 *     summary: Obtener configuración de notificaciones
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Configuración obtenida correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Configuration retrieved successfully"
 *                 data:
 *                   $ref: '#/components/schemas/NotificationsConfig'
 *             example:
 *               success: true
 *               message: "Configuration retrieved successfully"
 *               data:
 *                 email: true
 *                 sms: false
 *                 push: true
 *                 promotions: false
 *       404:
 *         description: Usuario no encontrado
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "User not found"
 *                 code:
 *                   type: string
 *                   example: "user_not_found"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 *   patch:
 *     operationId: editNotificationConfiguration
 *     tags: [Notifications]
 *     summary: Editar configuración de notificaciones
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: boolean
 *                 description: Recibir notificaciones por email
 *               sms:
 *                 type: boolean
 *                 description: Recibir notificaciones por SMS
 *               push:
 *                 type: boolean
 *                 description: Recibir notificaciones push
 *               promotions:
 *                 type: boolean
 *                 description: Recibir promociones
 *           example:
 *             email: true
 *             sms: false
 *             push: true
 *             promotions: false
 *     responses:
 *       200:
 *         description: Configuración actualizada correctamente
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               required: [success, message, data]
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Configuration updated successfully"
 *                 data:
 *                   type: object
 *                   properties:
 *                     userId:
 *                       type: string
 *             example:
 *               success: true
 *               message: "Configuration updated successfully"
 *               data:
 *                 userId: "665e3e2f2a9bd8c4b2c11111"
 *       401:
 *         $ref: "#/components/responses/Unauthorized"
 *       403:
 *         $ref: "#/components/responses/Forbidden"
 */
router.route("/configuration").get(getConfiguration).patch(editConfiguration);

export default router;
