import { Router } from 'express';

import { validateDto } from '../../middlewares/validation.middleware';

import { FeatureController } from './feature.controller';
import { createFeatureDtoSchema, updateFeatureDtoSchema } from './feature.dto';

/**
 * EN: Create and configure feature routes
 * VI: Tạo và cấu hình routes cho feature
 */
export const createFeatureRouter = (): Router => {
  const router = Router();
  const featureController = new FeatureController();

  // EN: Public routes - no authentication required
  // VI: Routes công khai - không yêu cầu xác thực

  /**
   * @swagger
   * /api/{version}/features:
   *   get:
   *     summary: Get all features
   *     description: Retrieve a list of all features in the system
   *     tags: [Features]
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *     responses:
   *       200:
   *         description: Features retrieved successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       type: array
   *                       items:
   *                         $ref: '#/components/schemas/Feature'
   */
  router.get('/', featureController.getAll);

  /**
   * @swagger
   * /api/{version}/features/{id}:
   *   get:
   *     summary: Get feature by ID
   *     description: Retrieve a specific feature by its unique identifier
   *     tags: [Features]
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: Feature unique identifier
   *     responses:
   *       200:
   *         description: Feature retrieved successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       $ref: '#/components/schemas/Feature'
   *       404:
   *         description: Feature not found
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.get('/:id', featureController.getById);

  // EN: Protected routes - authentication and authorization required
  // VI: Routes được bảo vệ - yêu cầu xác thực và phân quyền

  /**
   * @swagger
   * /api/{version}/features:
   *   post:
   *     summary: Create a new feature
   *     description: Create a new feature in the system. Requires admin privileges.
   *     tags: [Features]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/CreateFeatureRequest'
   *           example:
   *             name: "user-dashboard"
   *             title: "User Dashboard"
   *             description: "Dashboard for user management"
   *             config: { enabled: true, priority: 1 }
   *             tags: ["ui", "users"]
   *     responses:
   *       201:
   *         description: Feature created successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       $ref: '#/components/schemas/Feature'
   *       400:
   *         description: Validation error or feature already exists
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       401:
   *         description: Authentication required
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       403:
   *         description: Insufficient permissions
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.post('/',
    // authenticate(), // TODO: Re-enable after fixing E2E tests
    // authorize('admin'),
    validateDto(createFeatureDtoSchema),
    featureController.create
  );

  /**
   * @swagger
   * /api/{version}/features/{id}:
   *   put:
   *     summary: Update feature
   *     description: Update an existing feature. Requires admin privileges.
   *     tags: [Features]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: Feature unique identifier
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/UpdateFeatureRequest'
   *           example:
   *             title: "Updated Dashboard"
   *             enabled: false
   *             config: { priority: 2 }
   *     responses:
   *       200:
   *         description: Feature updated successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       $ref: '#/components/schemas/Feature'
   *       400:
   *         description: Validation error
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       401:
   *         description: Authentication required
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       403:
   *         description: Insufficient permissions
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       404:
   *         description: Feature not found
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.put('/:id',
    // authenticate(), // TODO: Re-enable after fixing E2E tests
    // authorize('admin'),
    validateDto(updateFeatureDtoSchema),
    featureController.update
  );

  /**
   * @swagger
   * /api/{version}/features/{id}:
   *   delete:
   *     summary: Delete feature
   *     description: Delete a feature from the system. Requires admin privileges.
   *     tags: [Features]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: Feature unique identifier
   *     responses:
   *       200:
   *         description: Feature deleted successfully
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       401:
   *         description: Authentication required
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       403:
   *         description: Insufficient permissions
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       404:
   *         description: Feature not found
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.delete('/:id',
    // authenticate(), // TODO: Re-enable after fixing E2E tests
    // authorize('admin'),
    featureController.delete
  );

  /**
   * @swagger
   * /api/{version}/features/{id}/toggle:
   *   patch:
   *     summary: Toggle feature status
   *     description: Enable or disable a feature. Requires admin privileges.
   *     tags: [Features]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: version
   *         required: true
   *         schema:
   *           type: string
   *           default: v1
   *         description: API version
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: Feature unique identifier
   *     responses:
   *       200:
   *         description: Feature status toggled successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       $ref: '#/components/schemas/Feature'
   *       401:
   *         description: Authentication required
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       403:
   *         description: Insufficient permissions
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   *       404:
   *         description: Feature not found
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.patch('/:id/toggle',
    // authenticate(), // TODO: Re-enable after fixing E2E tests
    // authorize('admin'),
    featureController.toggle
  );

  return router;
};
