/**
 * Regression: selectUnpickedAttendees must return attendees with NO MemberLunchPick
 * row at all, not just attendees whose pick row has pickedAt=null.
 *
 * Bug: the old cron filter used `lunchPick: { is: { pickedAt: null } }` which
 * only matches rows that exist but have pickedAt=null.  Attendees with no pick
 * row at all were silently skipped.
 */
import { afterAll, describe, expect, it } from 'vitest'
import { prisma } from '../setup'
import {
  createTestUser,
  createTestProgram,
  createTestProject,
  cleanupTestData,
  uid,
} from '../helpers'
import { selectUnpickedAttendees } from '@/server/services/lunch-reminders'

const programIds: string[] = []
const userIds: string[] = []

afterAll(async () => {
  for (const programId of programIds) {
    await prisma.memberLunchPick.deleteMany({
      where: { attendingMember: { confirmation: { project: { programId } } } },
    })
    await prisma.attendingMember.deleteMany({
      where: { confirmation: { project: { programId } } },
    })
    await prisma.finalistConfirmation.deleteMany({ where: { project: { programId } } })
    await prisma.lunchEvent.deleteMany({ where: { programId } })
    await cleanupTestData(programId, [])
  }
  if (userIds.length > 0) {
    await prisma.auditLog.deleteMany({ where: { userId: { in: userIds } } })
    await prisma.user.deleteMany({ where: { id: { in: userIds } } })
  }
})

describe('selectUnpickedAttendees', () => {
  it('returns attendees with no pick row AND unpicked rows; excludes picked', async () => {
    const program = await createTestProgram({ name: `rfilter-${uid()}` })
    programIds.push(program.id)

    // Three confirmed attendees on the same program
    const u1 = await createTestUser('APPLICANT') // no MemberLunchPick row at all
    const u2 = await createTestUser('APPLICANT') // MemberLunchPick with pickedAt=null
    const u3 = await createTestUser('APPLICANT') // MemberLunchPick with pickedAt set (PICKED)
    userIds.push(u1.id, u2.id, u3.id)

    const project = await createTestProject(program.id, {
      title: `rfilter-proj-${uid()}`,
      competitionCategory: 'STARTUP',
    })

    const conf = await prisma.finalistConfirmation.create({
      data: {
        projectId: project.id,
        category: 'STARTUP',
        status: 'CONFIRMED',
        deadline: new Date(Date.now() + 86_400_000),
        token: `tok-${uid()}`,
      },
    })

    const am1 = await prisma.attendingMember.create({
      data: { confirmationId: conf.id, userId: u1.id },
    })
    const am2 = await prisma.attendingMember.create({
      data: { confirmationId: conf.id, userId: u2.id },
    })
    const am3 = await prisma.attendingMember.create({
      data: { confirmationId: conf.id, userId: u3.id },
    })

    // am1: NO pick row
    // am2: pick row exists but pickedAt=null
    await prisma.memberLunchPick.create({
      data: { attendingMemberId: am2.id },
    })
    // am3: pick row with pickedAt set (has picked)
    await prisma.memberLunchPick.create({
      data: { attendingMemberId: am3.id, pickedAt: new Date() },
    })

    const event = await prisma.lunchEvent.create({
      data: { programId: program.id, enabled: true },
    })

    const result = await selectUnpickedAttendees(prisma, {
      id: event.id,
      programId: program.id,
    })

    const returnedIds = result.map((am) => am.id).sort()
    expect(returnedIds).toContain(am1.id)
    expect(returnedIds).toContain(am2.id)
    expect(returnedIds).not.toContain(am3.id)
    expect(result).toHaveLength(2)
  })

  it('excludes non-CONFIRMED confirmations', async () => {
    const program = await createTestProgram({ name: `rfilter-nc-${uid()}` })
    programIds.push(program.id)

    const u = await createTestUser('APPLICANT')
    userIds.push(u.id)

    const project = await createTestProject(program.id, {
      title: `rfilter-nc-${uid()}`,
      competitionCategory: 'STARTUP',
    })

    const conf = await prisma.finalistConfirmation.create({
      data: {
        projectId: project.id,
        category: 'STARTUP',
        status: 'PENDING', // NOT confirmed
        deadline: new Date(Date.now() + 86_400_000),
        token: `tok-${uid()}`,
      },
    })

    await prisma.attendingMember.create({
      data: { confirmationId: conf.id, userId: u.id },
    })

    const event = await prisma.lunchEvent.create({
      data: { programId: program.id, enabled: true },
    })

    const result = await selectUnpickedAttendees(prisma, {
      id: event.id,
      programId: program.id,
    })

    expect(result).toHaveLength(0)
  })
})
