const express = require('express');
const { body, query, param } = require('express-validator');
const adminNotificationController = require('../controllers/adminNotificationController');
const { authenticateAdmin } = require('../middleware/auth');
const { logAdminOperation } = require('../middleware/adminLogger');

const router = express.Router();

// All routes require admin authentication
router.use(authenticateAdmin);

/** GET /api/v1/admin/notifications - Get all notifications */
router.get('/',
  [
    query('page').optional().isInt({ min: 1 }),
    query('limit').optional().isInt({ min: 1, max: 100 }),
    query('type').optional().isIn(['system', 'activity', 'service']),
    query('userId').optional().isUUID(),
    query('isRead').optional().isIn(['true', 'false']),
    query('search').optional().isString(),
  ],
  logAdminOperation,
  adminNotificationController.getNotifications
);

/** GET /api/v1/admin/notifications/statistics - Get statistics */
router.get('/statistics', logAdminOperation, adminNotificationController.getStatistics);

/** POST /api/v1/admin/notifications/send - Send to specific user */
router.post('/send',
  [
    body('userId').notEmpty().withMessage('User ID is required'),
    body('titleZh').notEmpty().withMessage('Chinese title is required'),
    body('contentZh').notEmpty().withMessage('Chinese content is required'),
    body('type').optional().isIn(['system', 'activity', 'service']),
  ],
  logAdminOperation,
  adminNotificationController.sendNotification
);

/** POST /api/v1/admin/notifications/broadcast - Broadcast to all users */
router.post('/broadcast',
  [
    body('titleZh').notEmpty().withMessage('Chinese title is required'),
    body('contentZh').notEmpty().withMessage('Chinese content is required'),
    body('type').optional().isIn(['system', 'activity', 'service']),
  ],
  logAdminOperation,
  adminNotificationController.broadcastNotification
);

/** DELETE /api/v1/admin/notifications/:id - Delete notification */
router.delete('/:id',
  [param('id').isUUID().withMessage('Notification ID must be a valid UUID')],
  logAdminOperation,
  adminNotificationController.deleteNotification
);

module.exports = router;
