import { prisma } from '../../lib/prisma'
import { NotificationType, NotificationChannel } from '@rentaldrivego/database'

// ─── Company notifications ────────────────────────────────────

export function findCompany(companyId: string, unread?: string) {
  const where: any = { companyId, channel: 'IN_APP' }
  if (unread === 'true') where.readAt = null
  return prisma.notification.findMany({ where, orderBy: { createdAt: 'desc' }, take: 50 })
}

export function countUnread(companyId: string) {
  return prisma.notification.count({ where: { companyId, channel: 'IN_APP', readAt: null } })
}

export function markRead(id: string, companyId: string) {
  return prisma.notification.updateMany({ where: { id, companyId }, data: { readAt: new Date(), status: 'READ' } })
}

export function markAllRead(companyId: string) {
  return prisma.notification.updateMany({ where: { companyId, readAt: null }, data: { readAt: new Date(), status: 'READ' } })
}

export function findEmployeePreferences(employeeId: string) {
  return prisma.notificationPreference.findMany({ where: { employeeId } })
}

export async function upsertEmployeePreferences(employeeId: string, prefs: Array<{ notificationType: string; channel: string; enabled: boolean }>) {
  for (const pref of prefs) {
    await prisma.notificationPreference.upsert({
      where: { employeeId_notificationType_channel: { employeeId, notificationType: pref.notificationType as NotificationType, channel: pref.channel as NotificationChannel } },
      create: { employeeId, notificationType: pref.notificationType as NotificationType, channel: pref.channel as NotificationChannel, enabled: pref.enabled },
      update: { enabled: pref.enabled },
    })
  }
}

export function findCompanyHistory(
  companyId: string,
  opts?: { channel?: string; status?: string; limit?: number },
) {
  const where: any = { companyId }
  if (opts?.channel) where.channel = opts.channel
  if (opts?.status) where.status = opts.status
  return prisma.notification.findMany({
    where,
    orderBy: { createdAt: 'desc' },
    take: opts?.limit ?? 200,
  })
}

// ─── Renter notifications ─────────────────────────────────────

export function findRenter(renterId: string) {
  return prisma.notification.findMany({ where: { renterId, channel: 'IN_APP' }, orderBy: { createdAt: 'desc' }, take: 50 })
}

export function markRenterRead(id: string, renterId: string) {
  return prisma.notification.updateMany({ where: { id, renterId }, data: { readAt: new Date(), status: 'READ' } })
}

export function markAllRenterRead(renterId: string) {
  return prisma.notification.updateMany({ where: { renterId, channel: 'IN_APP', readAt: null }, data: { readAt: new Date(), status: 'READ' } })
}

export function findRenterPreferences(renterId: string) {
  return prisma.notificationPreference.findMany({ where: { renterId } })
}

export async function upsertRenterPreferences(renterId: string, prefs: Array<{ notificationType: string; channel: string; enabled: boolean }>) {
  for (const pref of prefs) {
    await prisma.notificationPreference.upsert({
      where: { renterId_notificationType_channel: { renterId, notificationType: pref.notificationType as NotificationType, channel: pref.channel as NotificationChannel } },
      create: { renterId, notificationType: pref.notificationType as NotificationType, channel: pref.channel as NotificationChannel, enabled: pref.enabled },
      update: { enabled: pref.enabled },
    })
  }
}
