import { Request, Response } from 'express';
import { notificationService, NotificationType, NotificationPriority } from './notificationService';

/**
 * Notification Controller
 * Handles HTTP requests for notification management
 */
export class NotificationController {
  /**
   * Get user's notifications
   */
  async getNotifications(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;
      const { page = 1, limit = 20, unreadOnly = false } = req.query;

      // This would normally query the database
      // For now, returning a placeholder response
      res.json({
        success: true,
        data: {
          notifications: [],
          pagination: {
            page: Number(page),
            limit: Number(limit),
            total: 0
          }
        }
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Mark notification as read
   */
  async markAsRead(req: Request, res: Response): Promise<void> {
    try {
      const { notificationId } = req.params;
      const userId = req.userId!;

      await notificationService.markNotificationAsRead(notificationId, userId);

      res.json({
        success: true,
        message: 'Notification marked as read'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Mark all notifications as read
   */
  async markAllAsRead(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;

      await notificationService.markAllNotificationsAsRead(userId);

      res.json({
        success: true,
        message: 'All notifications marked as read'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Get notification preferences
   */
  async getPreferences(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;

      // This would normally fetch from database
      // For now, returning default preferences
      res.json({
        success: true,
        data: {
          email: {
            enabled: true,
            frequency: 'instant',
            types: Object.values(NotificationType)
          },
          realtime: {
            enabled: true,
            types: Object.values(NotificationType),
            sound: true,
            desktop: true
          },
          doNotDisturb: {
            enabled: false
          }
        }
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Update notification preferences
   */
  async updatePreferences(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;
      const preferences = req.body;

      await notificationService.updateUserPreferences(userId, preferences);

      res.json({
        success: true,
        message: 'Preferences updated successfully'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Subscribe to notification types
   */
  async subscribe(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;
      const { types } = req.body;

      await notificationService.subscribeToNotificationTypes(userId, types);

      res.json({
        success: true,
        message: 'Subscribed to notification types'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Unsubscribe from notification types
   */
  async unsubscribe(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.userId!;
      const { types } = req.body;

      await notificationService.unsubscribeFromNotificationTypes(userId, types);

      res.json({
        success: true,
        message: 'Unsubscribed from notification types'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Send test notification (admin only)
   */
  async sendTestNotification(req: Request, res: Response): Promise<void> {
    try {
      const { userId, type, priority, title, message } = req.body;

      await notificationService.sendNotification(userId, {
        type: type || NotificationType.TASK_UPDATED,
        priority: priority || NotificationPriority.MEDIUM,
        title: title || 'Test Notification',
        message: message || 'This is a test notification'
      });

      res.json({
        success: true,
        message: 'Test notification sent'
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Clean up old notifications (admin only)
   */
  async cleanupNotifications(req: Request, res: Response): Promise<void> {
    try {
      const { daysToKeep = 30 } = req.body;

      const deleted = await notificationService.cleanupOldNotifications(daysToKeep);

      res.json({
        success: true,
        message: `Cleaned up ${deleted} old notifications`
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }
}