// Notification Storage Service
// Stores portal notifications in database

import prisma from '@/shared/database/prisma';
import { v4 as uuidv4 } from 'uuid';

export interface NotificationRecord {
  id: string;
  notificationId: string;
  recipientId: string;
  recipientType: 'email' | 'sms' | 'portal';
  template: string;
  subject?: string;
  body: string;
  variables: Record<string, any>;
  priority: 'low' | 'normal' | 'high' | 'urgent';
  status: 'pending' | 'sent' | 'failed' | 'read';
  sentAt?: Date;
  readAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export class NotificationStorageService {
  /**
   * Store portal notification
   */
  async storePortalNotification(
    recipientId: string,
    template: string,
    variables: Record<string, any>,
    priority: 'low' | 'normal' | 'high' | 'urgent' = 'normal'
  ): Promise<NotificationRecord> {
    const notificationId = `NOTIF-${uuidv4().substring(0, 8).toUpperCase()}`;

    const notification = await prisma.iruNotification.create({
      data: {
        id: uuidv4(),
        notificationId,
        recipientId,
        recipientType: 'portal',
        template,
        variables: variables as any,
        priority,
        status: 'pending',
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });

    return {
      id: notification.id,
      notificationId: notification.notificationId,
      recipientId: notification.recipientId,
      recipientType: notification.recipientType as 'email' | 'sms' | 'portal',
      template: notification.template,
      body: notification.body || '',
      variables: (notification.variables as any) || {},
      priority: notification.priority as 'low' | 'normal' | 'high' | 'urgent',
      status: notification.status as 'pending' | 'sent' | 'failed' | 'read',
      sentAt: notification.sentAt || undefined,
      readAt: notification.readAt || undefined,
      createdAt: notification.createdAt,
      updatedAt: notification.updatedAt,
    };
  }

  /**
   * Mark notification as read
   */
  async markAsRead(notificationId: string): Promise<void> {
    await prisma.iruNotification.update({
      where: { notificationId },
      data: {
        status: 'read',
        readAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Get notifications for recipient
   */
  async getNotifications(
    recipientId: string,
    options?: {
      status?: 'pending' | 'sent' | 'failed' | 'read';
      limit?: number;
      offset?: number;
    }
  ): Promise<NotificationRecord[]> {
    const notifications = await prisma.iruNotification.findMany({
      where: {
        recipientId,
        status: options?.status,
      },
      orderBy: {
        createdAt: 'desc',
      },
      take: options?.limit || 50,
      skip: options?.offset || 0,
    });

    return notifications.map((n) => ({
      id: n.id,
      notificationId: n.notificationId,
      recipientId: n.recipientId,
      recipientType: n.recipientType as 'email' | 'sms' | 'portal',
      template: n.template,
      subject: n.subject || undefined,
      body: n.body || '',
      variables: (n.variables as any) || {},
      priority: n.priority as 'low' | 'normal' | 'high' | 'urgent',
      status: n.status as 'pending' | 'sent' | 'failed' | 'read',
      sentAt: n.sentAt || undefined,
      readAt: n.readAt || undefined,
      createdAt: n.createdAt,
      updatedAt: n.updatedAt,
    }));
  }
}

export const notificationStorageService = new NotificationStorageService();
