import { notFound, redirect } from "next/navigation";
import AnnualSingleAuctionClient from "@/(pages)/autionClient/AnnualSingleAuctionClient";
import { getSingleAuctionById } from "@/actions/auction";

export default async function HorseDetailsPage({
  params,
  searchParams,
}: {
  params: Promise<{ lang: string }>;
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const [{ lang }, sp] = await Promise.all([params, searchParams]);

  const rawId = sp?.id;
  const id = typeof rawId === "string" ? rawId.trim() : "";

  if (!id) {
    notFound();
  }

  const rawGroupId = sp?.group_id;
  const groupId = typeof rawGroupId === "string" ? rawGroupId.trim() : "";
  if (groupId) {
    const item = await getSingleAuctionById(id);

    if (!item) {
      notFound();
    }
    return (
      <div dir="rtl" className="bg-[#FBF9F6] min-h-screen">
        <AnnualSingleAuctionClient item={item} />
      </div>
    );
  }

  const rawType = sp?.animal_type ?? sp?.type;
  const type = typeof rawType === "string" ? rawType : "";
  const base = type === "camel" ? "camels" : "horses";

  redirect(`/${lang}/${base}/${id}`);
}
