// app/admin/[entity]/page.tsx
import { notFound } from "next/navigation"

import { Header } from "@/components/header"
import { Main } from "@/components/main"
import { ProfileDropdown } from "@/components/profile-dropdown"
import { ThemeSwitch } from "@/components/theme-switch"
import { TableConfig } from "@/config/table"
import { getCab } from "@/services/cabs"
import { getFood } from "@/services/foods"
import { getHotel } from "@/services/hotels"
import { EntityForm } from "../../../form/entity-form"
import { getFieldsByEntity } from "@/services/myfields"

export default async function AdminEditPage({
  params,
}: {
  params: Promise<{ entity: string; id: string }>
}) {
  const { entity, id } = await params

  const staticFields = TableConfig[entity as keyof typeof TableConfig].fields
  const myfields = await getFieldsByEntity(entity)

  const mergedFields = [...staticFields, ...myfields]

  let values: Record<string, any> | null = null

  switch (entity) {
    case "hotels":
      values = await getHotel({ id })
      break
    case "cabs":
      values = await getCab({ id })
      break
    case "foods":
      values = await getFood({ id })
      break
    default:
      notFound()
  }

  if (!values) {
    notFound()
  }

  return (
    <>
      {/* ===== Top Heading ===== */}
      <Header>
        <h2 className="text-lg font-semibold">
          {entity.charAt(0).toUpperCase() + entity.slice(1)}
        </h2>

        <div className="ms-auto flex items-center space-x-4">
          <ThemeSwitch />
          <ProfileDropdown />
        </div>
      </Header>

      {/* ===== Main ===== */}
      <Main className="flex flex-1 flex-col gap-4 sm:gap-6">
        <div className="flex flex-wrap items-end justify-between gap-2">
          <div>
            <h2 className="text-2xl font-bold tracking-tight">
              Edit {entity.charAt(0).toUpperCase() + entity.slice(1)}
            </h2>
            <p className="text-muted-foreground">
              Here&apos;s a fields of your <strong>{entity} </strong>with their
              details and actions you can perform on them.
            </p>
          </div>
        </div>
        <EntityForm entity={entity} fields={mergedFields} values={values} />
      </Main>
    </>
  )
}
