import { NOTIFICATION_EVENTS } from '../../../../shared/constants/notification-events.js'
import { prisma } from '../prisma.js'

const {
  ORDER_CREATED,
  ORDER_STATUS_CHANGED,
  ORDER_MESSAGE_SENT,
  ORDER_MESSAGE_ADMIN_REPLY,
  PAYMENT_STATUS_CHANGED,
  DELIVERY_FEE_ADJUSTED,
} = NOTIFICATION_EVENTS

const userEventFieldMap = {
  [ORDER_CREATED]: 'orderCreated',
  [ORDER_STATUS_CHANGED]: 'orderStatusChanged',
  [ORDER_MESSAGE_ADMIN_REPLY]: 'orderMessageReceived',
  [PAYMENT_STATUS_CHANGED]: 'paymentStatusChanged',
  [DELIVERY_FEE_ADJUSTED]: 'deliveryFeeAdjusted',
}

const adminEventFieldMap = {
  [ORDER_MESSAGE_SENT]: 'newOrderMessage',
  'review:created': 'newReview',
}

export async function resolveUserNotificationTargets(eventType, payload) {
  const targets = []

  if (payload.userId) {
    const prefs = await prisma.notificationPreference.findUnique({
      where: { userId: payload.userId },
    })

    if (prefs && prefs.globalEnabled) {
      const field = userEventFieldMap[eventType]
      if (field && prefs[field]) {
        const user = await prisma.user.findUnique({
          where: { id: payload.userId },
          select: { email: true },
        })
        if (user) {
          targets.push({ channel: 'email', recipient: user.email })
        }
      }
    }
  }

  return targets
}

export async function resolveAdminNotificationTargets(eventType, payload) {
  const targets = []
  const settings = await prisma.adminNotificationSettings.findFirst()
  if (!settings) return targets

  const field = adminEventFieldMap[eventType]
  if (field === 'newReview') {
    if (!settings.newReview) return targets
  } else if (field && !settings[field]) {
    return targets
  }

  if (settings.emailEnabled) {
    const admin = await prisma.user.findFirst({
      where: { email: process.env.ADMIN_EMAIL },
      select: { email: true },
    })
    if (admin) {
      targets.push({ channel: 'email', recipient: admin.email })
    }
  }

  if (settings.telegramEnabled && settings.telegramChatId) {
    targets.push({ channel: 'telegram', recipient: settings.telegramChatId })
  }

  return targets
}

export async function resolveAuthCodeTargets(eventType, payload) {
  const targets = []

  if (payload.email) {
    targets.push({ channel: 'email', recipient: payload.email })
  }

  if (payload.isAdmin) {
    const settings = await prisma.adminNotificationSettings.findFirst()
    if (settings && settings.telegramEnabled && settings.telegramChatId && settings.authCodeDuplicate) {
      targets.push({ channel: 'telegram', recipient: settings.telegramChatId })
    }
  }

  return targets
}

export async function ensureUserNotificationPreference(userId) {
  const existing = await prisma.notificationPreference.findUnique({
    where: { userId },
  })
  if (existing) return existing
  return prisma.notificationPreference.create({
    data: { userId, globalEnabled: true },
  })
}
