import { prisma } from '@/lib/prisma'
import { config } from '../config/app.config'
import { processDownload } from './processor'

export async function pollOnce(): Promise<void> {
  const pending = await prisma.download.findMany({
    where: { status: 'PENDING' },
    orderBy: { submittedAt: 'asc' },
    take: config.WORKER_CONCURRENCY,
    select: { id: true },
  })

  if (pending.length === 0) return

  const ids = pending.map((d) => d.id)

  await prisma.download.updateMany({
    where: { id: { in: ids }, status: 'PENDING' },
    data: { status: 'PROCESSING', startedAt: new Date() },
  })

  await Promise.all(ids.map((id) => processDownload(id)))
}

export async function runWorker(): Promise<never> {
  console.log('[worker] started')
  while (true) {
    try {
      await pollOnce()
    } catch (err) {
      console.error('[worker] poll error:', err)
    }
    await new Promise((resolve) =>
      setTimeout(resolve, config.WORKER_POLL_INTERVAL_MS)
    )
  }
}

if (require.main === module) {
  runWorker().catch(console.error)
}
