import { authenticateApiRequest } from '@/lib/api-auth'
import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'
import { storyStatusToApi, taskStatusToApi } from '@/lib/task-status'

export async function GET(
  request: Request,
  { params }: { params: Promise<{ id: string }> },
) {
  const auth = await authenticateApiRequest(request)
  if ('error' in auth) {
    return Response.json({ error: auth.error }, { status: auth.status })
  }

  const { id } = await params

  const product = await prisma.product.findFirst({
    where: { id, ...productAccessFilter(auth.userId) },
    select: {
      id: true,
      code: true,
      name: true,
      description: true,
      repo_url: true,
      definition_of_done: true,
    },
  })
  if (!product) {
    return Response.json({ error: 'Product niet gevonden' }, { status: 404 })
  }

  const [activeSprint, openIdeas] = await Promise.all([
    prisma.sprint.findFirst({
      where: { product_id: id, status: 'OPEN' },
      select: { id: true, sprint_goal: true, status: true },
    }),
    prisma.idea.findMany({
      where: {
        user_id: auth.userId,
        product_id: id,
        archived: false,
        status: { not: 'PLANNED' },
      },
      select: {
        id: true,
        code: true,
        title: true,
        description: true,
        status: true,
        created_at: true,
      },
      orderBy: { created_at: 'asc' },
      take: 50,
    }),
  ])

  let nextStoryPayload: unknown = null
  if (activeSprint) {
    const story = await prisma.story.findFirst({
      where: { sprint_id: activeSprint.id, status: 'IN_SPRINT' },
      orderBy: [{ sort_order: 'asc' }],
      include: {
        tasks: {
          orderBy: { sort_order: 'asc' },
          select: {
            id: true,
            title: true,
            description: true,
            implementation_plan: true,
            priority: true,
            sort_order: true,
            status: true,
          },
        },
      },
    })
    if (story) {
      nextStoryPayload = {
        id: story.id,
        code: story.code,
        title: story.title,
        description: story.description,
        acceptance_criteria: story.acceptance_criteria,
        priority: story.priority,
        status: storyStatusToApi(story.status),
        tasks: story.tasks.map((t, idx) => ({
          id: t.id,
          code: story.code ? `${story.code}.${idx + 1}` : null,
          title: t.title,
          description: t.description,
          implementation_plan: t.implementation_plan,
          priority: t.priority,
          sort_order: t.sort_order,
          status: taskStatusToApi(t.status),
        })),
      }
    }
  }

  return Response.json({
    product,
    active_sprint: activeSprint,
    next_story: nextStoryPayload,
    open_ideas: openIdeas,
  })
}
