const express = require('express');
const router = express.Router();
const insuranceTypeController = require('../controllers/insuranceTypeController');
const { jwtAuth, checkPermission } = require('../middleware/auth');

/**
 * @swagger
 * /api/insurance-types:
 *   get:
 *     summary: 获取险种列表
 *     description: 分页获取险种列表，支持多种筛选条件
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - name: page
 *         in: query
 *         description: 页码
 *         required: false
 *         schema:
 *           type: integer
 *           default: 1
 *           minimum: 1
 *       - name: pageSize
 *         in: query
 *         description: 每页数量
 *         required: false
 *         schema:
 *           type: integer
 *           default: 10
 *           minimum: 1
 *           maximum: 100
 *       - name: name
 *         in: query
 *         description: 险种名称（模糊搜索）
 *         required: false
 *         schema:
 *           type: string
 *       - name: status
 *         in: query
 *         description: 险种状态
 *         required: false
 *         schema:
 *           type: string
 *           enum: [active, inactive]
 *       - name: applicable_livestock
 *         in: query
 *         description: 适用牲畜类型（模糊搜索）
 *         required: false
 *         schema:
 *           type: string
 *       - name: service_area
 *         in: query
 *         description: 服务区域（模糊搜索）
 *         required: false
 *         schema:
 *           type: string
 *       - name: on_sale_status
 *         in: query
 *         description: 在售状态
 *         required: false
 *         schema:
 *           type: boolean
 *     responses:
 *       '200':
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "获取险种列表成功"
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/InsuranceType'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '500':
 *         description: 服务器内部错误
 *   post:
 *     summary: 创建险种
 *     description: 创建新的保险险种
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreateInsuranceTypeRequest'
 *     responses:
 *       '201':
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "创建险种成功"
 *                 data:
 *                   $ref: '#/components/schemas/InsuranceType'
 *       '400':
 *         description: 请求参数错误
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '500':
 *         description: 服务器内部错误
 */

// 获取险种列表
router.get('/', jwtAuth, checkPermission('insurance_type', 'read'), 
  insuranceTypeController.getInsuranceTypes
);

/**
 * @swagger
 * /api/insurance-types/{id}:
 *   get:
 *     summary: 获取单个险种
 *     description: 根据ID获取险种详细信息
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - name: id
 *         in: path
 *         description: 险种ID
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       '200':
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "获取险种详情成功"
 *                 data:
 *                   $ref: '#/components/schemas/InsuranceType'
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '404':
 *         description: 险种不存在
 *       '500':
 *         description: 服务器内部错误
 */
// 导出险种列表到Excel
router.get('/export', jwtAuth, checkPermission('insurance_type', 'read'), insuranceTypeController.exportToExcel);

// 获取单个险种详情
router.get('/:id', jwtAuth, checkPermission('insurance_type', 'read'),
  insuranceTypeController.getInsuranceTypeById
);

// 创建险种
router.post('/', jwtAuth, checkPermission('insurance_type', 'create'), insuranceTypeController.createInsuranceType);

/**
 * @swagger
 * /api/insurance-types/{id}:
 *   put:
 *     summary: 更新险种
 *     description: 根据ID更新险种信息
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - name: id
 *         in: path
 *         description: 险种ID
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/UpdateInsuranceTypeRequest'
 *     responses:
 *       '200':
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "更新险种成功"
 *                 data:
 *                   $ref: '#/components/schemas/InsuranceType'
 *       '400':
 *         description: 请求参数错误
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '404':
 *         description: 险种不存在
 *       '500':
 *         description: 服务器内部错误
 *   delete:
 *     summary: 删除险种
 *     description: 根据ID删除险种
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - name: id
 *         in: path
 *         description: 险种ID
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       '200':
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "删除险种成功"
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '404':
 *         description: 险种不存在
 *       '500':
 *         description: 服务器内部错误
 */
// 更新险种
router.put('/:id', jwtAuth, checkPermission('insurance_type', 'update'), insuranceTypeController.updateInsuranceType);

// 删除险种
router.delete('/:id', jwtAuth, checkPermission('insurance_type', 'delete'), insuranceTypeController.deleteInsuranceType);

/**
 * @swagger
 * /api/insurance-types/{id}/status:
 *   patch:
 *     summary: 更新险种状态
 *     description: 更新险种的状态和在售状态
 *     tags:
 *       - 险种管理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - name: id
 *         in: path
 *         description: 险种ID
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/UpdateInsuranceTypeStatusRequest'
 *     responses:
 *       '200':
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "更新险种状态成功"
 *                 data:
 *                   $ref: '#/components/schemas/InsuranceType'
 *       '400':
 *         description: 请求参数错误
 *       '401':
 *         $ref: '#/components/responses/UnauthorizedError'
 *       '403':
 *         $ref: '#/components/responses/ForbiddenError'
 *       '404':
 *         description: 险种不存在
 *       '500':
 *         description: 服务器内部错误
 */
// 更新险种状态
router.patch('/:id/status', jwtAuth, checkPermission('insurance_type', 'update'), insuranceTypeController.updateInsuranceTypeStatus);

module.exports = router;