"use server"

import { getI18n } from "@/i18n/server"
import { AssetService } from "@/services/asset.service"
import { ItemService } from "@/services/item.service"
import { RecipientService } from "@/services/recipient.service"
import type { AssetWithAssignment } from "@/types"

import EditAssetForm from "../../_components/edit.asset.form"

export default async function EditAssetPage({
  params,
}: {
  params: Promise<{ assetId: string }>
}) {
  const { assetId } = await params
  const items = await ItemService.findAll()
  const recipients = await RecipientService.findAll()
  const asset = await AssetService.findById(assetId)
  const { dictionary } = await getI18n()
  const copy = dictionary.inventory.assets

  if (!asset) {
    return <div>{copy.edit.notFound}</div>
  }

  return (
    <div className="flex flex-col gap-4">
      <div className="flex items-center justify-between gap-4">
        <h1 className="text-2xl font-bold">{copy.edit.title}</h1>
      </div>
      <EditAssetForm
        items={items}
        recipients={recipients}
        asset={asset as unknown as AssetWithAssignment}
        formCopy={copy.form}
        schemaCopy={copy.schema}
        statusCopy={copy.status}
        submitButtonCopy={dictionary.common.submitButton}
      />
    </div>
  )
}
