import { NextResponse, type NextRequest } from 'next/server'
import { prisma } from '@/lib/prisma'
import { sendLunchReminderEmail } from '@/lib/email'
import {
  selectUnpickedAttendees,
  selectUnpickedExternals,
} from '@/server/services/lunch-reminders'
import { sendExternalDishInvite } from '@/server/services/lunch-external-invite'

/**
 * Cron: send a single reminder email per attending member who hasn't picked
 * a lunch dish yet, when we're inside the reminder window
 * (deadline - reminderHoursBeforeDeadline) <= now < deadline.
 *
 * Idempotent — `LunchEvent.reminderSentAt` blocks repeat sends.
 */
export async function GET(request: NextRequest): Promise<NextResponse> {
  const cronSecret = request.headers.get('x-cron-secret')
  if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }
  const now = new Date()
  const events = await prisma.lunchEvent.findMany({
    where: {
      enabled: true,
      reminderSentAt: null,
      reminderHoursBeforeDeadline: { not: null },
      eventAt: { not: null },
    },
  })
  let sent = 0
  for (const event of events) {
    try {
      if (!event.eventAt || event.reminderHoursBeforeDeadline == null) continue
      const deadline = new Date(
        event.eventAt.getTime() - event.changeCutoffHours * 3_600_000,
      )
      const reminderAt = new Date(
        deadline.getTime() - event.reminderHoursBeforeDeadline * 3_600_000,
      )
      if (now < reminderAt || now >= deadline) continue

      const ams = await selectUnpickedAttendees(prisma, event)
      for (const am of ams) {
        if (!am.user.email) continue
        try {
          await sendLunchReminderEmail({
            to: am.user.email,
            memberName: am.user.name ?? am.user.email,
            eventAt: event.eventAt,
            venue: event.venue,
            changeDeadline: deadline,
            pickUrl: `${process.env.NEXTAUTH_URL ?? ''}/applicant`,
          })
          sent++
        } catch (e) {
          console.error('[lunch-reminders] send failed for', am.user.email, e)
        }
      }

      // External attendees: emailed + no dish yet → their tokenized pick page.
      const externals = await selectUnpickedExternals(prisma, { id: event.id })
      for (const ext of externals) {
        if (!ext.email) continue
        try {
          await sendExternalDishInvite(prisma, ext, event)
          sent++
        } catch (e) {
          console.error('[lunch-reminders] external send failed for', ext.email, e)
        }
      }

      await prisma.lunchEvent.update({
        where: { id: event.id },
        data: { reminderSentAt: new Date() },
      })
    } catch (e) {
      console.error('[lunch-reminders] event failed', event.id, e)
    }
  }
  return NextResponse.json({ ok: true, sent, processedEvents: events.length })
}
