import { Request, Response } from 'express';
import NotificationsService from '../services/notificationsService';

class NotificationsController {
  private notificationsService: NotificationsService;

  constructor() {
    this.notificationsService = new NotificationsService();
  }

  public async sendEmailTrigger(req: Request, res: Response): Promise<void> {
    try {
      const { email, subject, message } = req.body;
      await this.notificationsService.sendEmail(email, subject, message);
      res.status(200).json({ message: 'Email sent successfully' });
    } catch (error) {
      res.status(500).json({ error: 'Failed to send email' });
    }
  }

  public async createInAppAlert(req: Request, res: Response): Promise<void> {
    try {
      const { userId, alertMessage } = req.body;
      await this.notificationsService.createInAppAlert(userId, alertMessage);
      res.status(201).json({ message: 'In-app alert created successfully' });
    } catch (error) {
      res.status(500).json({ error: 'Failed to create in-app alert' });
    }
  }

  public async sendPushNotification(req: Request, res: Response): Promise<void> {
    try {
      const { userId, notificationMessage } = req.body;
      await this.notificationsService.sendPushNotification(userId, notificationMessage);
      res.status(200).json({ message: 'Push notification sent successfully' });
    } catch (error) {
      res.status(500).json({ error: 'Failed to send push notification' });
    }
  }
}

export default NotificationsController;