const express = require('express');
const router = express.Router();
const menuCategoryController = require('../controllers/menuCategoryController');

/**
 * @swagger
 * components:
 *   schemas:
 *     MenuCategory:
 *       type: object
 *       required:
 *         - nom
 *       properties:
 *         id:
 *           type: integer
 *           description: Auto-generated ID
 *         nom:
 *           type: string
 *           maxLength: 100
 *           description: Category name
 *         description:
 *           type: string
 *           description: Category description
 *         ordre:
 *           type: integer
 *           default: 0
 *           description: Display order
 *         actif:
 *           type: boolean
 *           default: true
 *           description: Active status
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: Creation timestamp
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: Last update timestamp
 *       example:
 *         id: 1
 *         nom: "Entrées"
 *         description: "Plats d'entrée"
 *         ordre: 1
 *         actif: true
 *         created_at: "2024-01-01T10:00:00Z"
 *         updated_at: "2024-01-01T10:00:00Z"
 *
 *     MenuCategoryInput:
 *       type: object
 *       required:
 *         - nom
 *       properties:
 *         nom:
 *           type: string
 *           maxLength: 100
 *           description: Category name
 *         description:
 *           type: string
 *           description: Category description
 *         ordre:
 *           type: integer
 *           default: 0
 *           description: Display order
 *         actif:
 *           type: boolean
 *           default: true
 *           description: Active status
 *       example:
 *         nom: "Desserts"
 *         description: "Desserts et pâtisseries"
 *         ordre: 3
 *         actif: true
 *
 *     Error:
 *       type: object
 *       properties:
 *         success:
 *           type: boolean
 *           example: false
 *         message:
 *           type: string
 *           example: "Error message"
 *         error:
 *           type: string
 *           example: "Detailed error information"
 */

/**
 * @swagger
 * tags:
 *   name: Menu Categories
 *   description: Menu category management endpoints
 */

/**
 * @swagger
 * /api/menu-categories:
 *   get:
 *     summary: Get all menu categories
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: Page number for pagination
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: Number of items per page
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: Search term for category name or description
 *       - in: query
 *         name: actif
 *         schema:
 *           type: boolean
 *         description: Filter by active status
 *       - in: query
 *         name: sort_by
 *         schema:
 *           type: string
 *           enum: [nom, ordre, created_at]
 *           default: ordre
 *         description: Field to sort by
 *       - in: query
 *         name: sort_order
 *         schema:
 *           type: string
 *           enum: [ASC, DESC]
 *           default: ASC
 *         description: Sort order
 *     responses:
 *       200:
 *         description: List of menu categories retrieved successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     categories:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/MenuCategory'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         currentPage:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *                         totalItems:
 *                           type: integer
 *                         itemsPerPage:
 *                           type: integer
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/', menuCategoryController.getAllCategories);

/**
 * @swagger
 * /api/menu-categories/active:
 *   get:
 *     summary: Get all active menu categories (simplified)
 *     tags: [Menu Categories]
 *     responses:
 *       200:
 *         description: List of active menu categories
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/MenuCategory'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/active', menuCategoryController.getActiveCategories);

/**
 * @swagger
 * /api/menu-categories/stats:
 *   get:
 *     summary: Get menu categories statistics
 *     tags: [Menu Categories]
 *     responses:
 *       200:
 *         description: Categories statistics
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: Total number of categories
 *                     active:
 *                       type: integer
 *                       description: Number of active categories
 *                     inactive:
 *                       type: integer
 *                       description: Number of inactive categories
 *                     totalMenus:
 *                       type: integer
 *                       description: Total number of menus across all categories
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/stats', menuCategoryController.getCategoryStats);

/**
 * @swagger
 * /api/menu-categories/{id}:
 *   get:
 *     summary: Get menu category by ID
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: Category ID
 *     responses:
 *       200:
 *         description: Menu category found
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/MenuCategory'
 *       404:
 *         description: Category not found
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/:id', menuCategoryController.getCategoryById);

/**
 * @swagger
 * /api/menu-categories/{id}/menus:
 *   get:
 *     summary: Get all menus in a category
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: Category ID
 *       - in: query
 *         name: actif
 *         schema:
 *           type: boolean
 *         description: Filter menus by active status
 *     responses:
 *       200:
 *         description: Menus in category retrieved successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     category:
 *                       $ref: '#/components/schemas/MenuCategory'
 *                     menus:
 *                       type: array
 *                       items:
 *                         type: object
 *       404:
 *         description: Category not found
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/:id/menus', menuCategoryController.getCategoryMenus);

/**
 * @swagger
 * /api/menu-categories:
 *   post:
 *     summary: Create a new menu category
 *     tags: [Menu Categories]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/MenuCategoryInput'
 *     responses:
 *       201:
 *         description: Category created successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Catégorie créée avec succès"
 *                 data:
 *                   $ref: '#/components/schemas/MenuCategory'
 *       400:
 *         description: Validation error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/', menuCategoryController.createCategory);

/**
 * @swagger
 * /api/menu-categories/{id}:
 *   put:
 *     summary: Update menu category
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: Category ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/MenuCategoryInput'
 *     responses:
 *       200:
 *         description: Category updated successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Catégorie mise à jour avec succès"
 *                 data:
 *                   $ref: '#/components/schemas/MenuCategory'
 *       400:
 *         description: Validation error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       404:
 *         description: Category not found
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.put('/:id', menuCategoryController.updateCategory);

/**
 * @swagger
 * /api/menu-categories/{id}/toggle-status:
 *   patch:
 *     summary: Toggle category active status
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: Category ID
 *     responses:
 *       200:
 *         description: Status toggled successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Statut mis à jour avec succès"
 *                 data:
 *                   $ref: '#/components/schemas/MenuCategory'
 *       404:
 *         description: Category not found
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.patch('/:id/toggle-status', menuCategoryController.toggleCategoryStatus);

/**
 * @swagger
 * /api/menu-categories/reorder:
 *   patch:
 *     summary: Reorder categories
 *     tags: [Menu Categories]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               categories:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: integer
 *                     ordre:
 *                       type: integer
 *                 example:
 *                   - id: 1
 *                     ordre: 1
 *                   - id: 2
 *                     ordre: 2
 *     responses:
 *       200:
 *         description: Categories reordered successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Ordre des catégories mis à jour avec succès"
 *       400:
 *         description: Validation error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.patch('/reorder', menuCategoryController.reorderCategories);

/**
 * @swagger
 * /api/menu-categories/{id}:
 *   delete:
 *     summary: Delete menu category
 *     tags: [Menu Categories]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: Category ID
 *     responses:
 *       200:
 *         description: Category deleted successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "Catégorie supprimée avec succès"
 *       400:
 *         description: Cannot delete category with associated menus
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       404:
 *         description: Category not found
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.delete('/:id', menuCategoryController.deleteCategory);

module.exports = router;
