const { validationResult } = require('express-validator');
const AdminNotificationService = require('../services/adminNotificationService');
const logger = require('../config/logger');

/**
 * Admin Notification Controller
 */
class AdminNotificationController {
  /** GET /api/v1/admin/notifications */
  static async getNotifications(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ code: 400, message: 'Validation error', data: null, errors: errors.array() });
      }
      const { page = 1, limit = 20, type, userId, isRead, search } = req.query;
      const result = await AdminNotificationService.getNotifications({ page: parseInt(page), limit: parseInt(limit), type, userId, isRead, search });
      res.json({ code: 0, message: 'Success', ...result });
    } catch (error) {
      logger.error('Error in getNotifications (admin):', error);
      next(error);
    }
  }

  /** POST /api/v1/admin/notifications/send */
  static async sendNotification(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ code: 400, message: 'Validation error', data: null, errors: errors.array() });
      }
      const { userId, titleZh, titleEn, titlePt, contentZh, contentEn, contentPt, type } = req.body;
      const notification = await AdminNotificationService.sendToUser(userId, { titleZh, titleEn, titlePt, contentZh, contentEn, contentPt, type });
      res.status(201).json({ code: 0, message: 'Notification sent successfully', data: notification });
    } catch (error) {
      logger.error('Error in sendNotification:', error);
      next(error);
    }
  }

  /** POST /api/v1/admin/notifications/broadcast */
  static async broadcastNotification(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({ code: 400, message: 'Validation error', data: null, errors: errors.array() });
      }
      const { titleZh, titleEn, titlePt, contentZh, contentEn, contentPt, type } = req.body;
      const result = await AdminNotificationService.broadcast({ titleZh, titleEn, titlePt, contentZh, contentEn, contentPt, type });
      res.status(201).json({ code: 0, message: `Notification broadcast to ${result.count} users`, data: result });
    } catch (error) {
      logger.error('Error in broadcastNotification:', error);
      next(error);
    }
  }

  /** DELETE /api/v1/admin/notifications/:id */
  static async deleteNotification(req, res, next) {
    try {
      const { id } = req.params;
      await AdminNotificationService.deleteNotification(id);
      res.json({ code: 0, message: 'Notification deleted successfully', data: null });
    } catch (error) {
      logger.error('Error in deleteNotification (admin):', error);
      next(error);
    }
  }

  /** GET /api/v1/admin/notifications/statistics */
  static async getStatistics(req, res, next) {
    try {
      const stats = await AdminNotificationService.getStatistics();
      res.json({ code: 0, message: 'Success', data: stats });
    } catch (error) {
      logger.error('Error in getStatistics:', error);
      next(error);
    }
  }
}

module.exports = AdminNotificationController;
