import { NextRequest } from 'next/server'
import { boardActivity } from '@/lib/data/dev-tasks'
import { apiActor, noStore } from '@/lib/integrations/dev-task-auth'

export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'

/** What happened on the board since a time: tasks started, completed, commented. Newest first. */
export async function GET(req: NextRequest) {
  const auth = apiActor(req)
  if (!auth.ok) return auth.response
  const since = req.nextUrl.searchParams.get('since')
  if (since && Number.isNaN(Date.parse(since))) return noStore({ error: 'since must be an ISO date-time.' }, 400)
  const result = await boardActivity(since || undefined, Number(req.nextUrl.searchParams.get('limit')) || 200)
  return result.ok ? noStore({ events: result.value }) : noStore({ error: result.error }, result.status)
}
