import type { UpdateAssignmentFormType } from "@/lib/schemas/assignment.schemas"
import type { Item } from "@/lib/types"
import { AssetService } from "@/services/asset.service"
import { AssignmentService } from "@/services/assignment.service"
import { ItemService } from "@/services/item.service"
import { RecipientService } from "@/services/recipient.service"

import AssignmentForm from "../../_components/edit.assignment.form"
export default async function EditAssignmentPage({
  params,
}: {
  params: Promise<{ assignamentId: string }>
}) {
  const { assignamentId } = await params
  const assignment = await AssignmentService.findById(assignamentId)
  const recipients = await RecipientService.findAll()
  const items = await ItemService.findAllWithStock()
  const assets = await AssetService.findAll()

  if (!assignment) {
    return <div>Assignment not found</div>
  }

  let assignmentItem: Item = {} as Item

  if (assignment.itemId) {
    assignmentItem = (await ItemService.findById(assignment.itemId)) as Item
    items.push(assignmentItem)
  }

  return (
    <div>
      <AssignmentForm
        recipients={recipients}
        items={items}
        assets={assets}
        initialData={assignment as UpdateAssignmentFormType}
      />
    </div>
  )
}
