/**
 * @ai-summary Notifications feature routes
 * @ai-context Registers notification API endpoints with proper guards
 */

import { FastifyPluginAsync } from 'fastify';
import { NotificationsController } from './notifications.controller';
import type {
  TemplateKeyParam,
  UpdateEmailTemplateRequest,
  PreviewTemplateRequest
} from './notifications.validation';

export const notificationsRoutes: FastifyPluginAsync = async (fastify) => {
  const controller = new NotificationsController();

  // ========================
  // User Endpoints
  // ========================

  // GET /api/notifications/summary - Get notification summary for login toast
  fastify.get('/notifications/summary', {
    preHandler: [fastify.authenticate],
    handler: controller.getSummary.bind(controller)
  });

  // GET /api/notifications/maintenance - Get due maintenance items
  fastify.get('/notifications/maintenance', {
    preHandler: [fastify.authenticate],
    handler: controller.getDueMaintenanceItems.bind(controller)
  });

  // GET /api/notifications/documents - Get expiring documents
  fastify.get('/notifications/documents', {
    preHandler: [fastify.authenticate],
    handler: controller.getExpiringDocuments.bind(controller)
  });

  // ========================
  // In-App Notifications
  // ========================

  // GET /api/notifications/in-app - Get user's in-app notifications
  fastify.get('/notifications/in-app', {
    preHandler: [fastify.authenticate],
    handler: controller.getInAppNotifications.bind(controller)
  });

  // GET /api/notifications/in-app/count - Get unread notification count
  fastify.get('/notifications/in-app/count', {
    preHandler: [fastify.authenticate],
    handler: controller.getUnreadCount.bind(controller)
  });

  // PUT /api/notifications/in-app/:id/read - Mark single notification as read
  fastify.put<{ Params: { id: string } }>('/notifications/in-app/:id/read', {
    preHandler: [fastify.authenticate],
    handler: controller.markAsRead.bind(controller)
  });

  // PUT /api/notifications/in-app/read-all - Mark all notifications as read
  fastify.put('/notifications/in-app/read-all', {
    preHandler: [fastify.authenticate],
    handler: controller.markAllAsRead.bind(controller)
  });

  // DELETE /api/notifications/in-app/:id - Delete a notification
  fastify.delete<{ Params: { id: string } }>('/notifications/in-app/:id', {
    preHandler: [fastify.authenticate],
    handler: controller.deleteNotification.bind(controller)
  });

  // ========================
  // Admin Endpoints
  // ========================

  // GET /api/admin/email-templates - List all email templates
  fastify.get('/admin/email-templates', {
    preHandler: [fastify.requireAdmin],
    handler: controller.listTemplates.bind(controller)
  });

  // GET /api/admin/email-templates/:key - Get single email template
  fastify.get<{ Params: TemplateKeyParam }>('/admin/email-templates/:key', {
    preHandler: [fastify.requireAdmin],
    handler: controller.getTemplate.bind(controller)
  });

  // PUT /api/admin/email-templates/:key - Update email template
  fastify.put<{
    Params: TemplateKeyParam;
    Body: UpdateEmailTemplateRequest;
  }>('/admin/email-templates/:key', {
    preHandler: [fastify.requireAdmin],
    handler: controller.updateTemplate.bind(controller)
  });

  // POST /api/admin/email-templates/:key/preview - Preview template with variables
  fastify.post<{
    Params: TemplateKeyParam;
    Body: PreviewTemplateRequest;
  }>('/admin/email-templates/:key/preview', {
    preHandler: [fastify.requireAdmin],
    handler: controller.previewTemplate.bind(controller)
  });

  // POST /api/admin/email-templates/:key/test - Send test email to admin
  fastify.post<{ Params: TemplateKeyParam }>('/admin/email-templates/:key/test', {
    preHandler: [fastify.requireAdmin],
    handler: controller.sendTestEmail.bind(controller)
  });
};
