import { redirect } from 'next/navigation'
import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'
import { TaskDialog } from './task-dialog'

interface EditTaskLoaderProps {
  taskId: string
  userId: string
  productId: string
  closePath: string
  isDemo: boolean
}

export async function EditTaskLoader({
  taskId,
  userId,
  productId,
  closePath,
  isDemo,
}: EditTaskLoaderProps) {
  const task = await prisma.task.findFirst({
    where: {
      id: taskId,
      story: { product: productAccessFilter(userId) },
    },
    select: {
      id: true,
      code: true,
      title: true,
      description: true,
      implementation_plan: true,
      priority: true,
      status: true,
      created_at: true,
    },
  })

  if (!task) redirect(closePath)

  return (
    <TaskDialog
      task={task}
      productId={productId}
      closePath={closePath}
      isDemo={isDemo}
    />
  )
}
