import { getAuctionById } from "@/actions/auction";
import { metaObject } from "@/config/site.config";
import AuctionClientCustom from "@/components/AuctionClientCustom";
import { getLocale, getTranslations } from "next-intl/server";

export async function generateMetadata({
  params,
}: {
  params: Promise<{ lang: string; id: string }>;
}) {
  const { id } = await params;
  const locale = await getLocale();
  const t = await getTranslations({
    locale,
    namespace: "AUCTION_DETAILS_PAGE",
  });
  let title = t("meta_title");

  try {
    const data = await getAuctionById(id);
    const auctionTitle = (data as any)?.title;
    if (auctionTitle) {
      title = auctionTitle;
    }
  } catch {
    // ignore metadata fetch errors
  }

  return metaObject(
    title,
    t("meta_description"),
    undefined,
    undefined,
    `/auctions/${id}`,
  );
}

export default async function AuctionDetailsPage({
  params,
}: {
  params: Promise<{ lang: string; id: string }>;
}) {
  const { id } = await params;
  const locale = await getLocale();
  const t = await getTranslations({
    locale,
    namespace: "AUCTION_DETAILS_PAGE",
  });
  const data = await getAuctionById(id);
  if (!data) {
    return (
      <div className="text-center py-20 text-red-600 font-bold">
        {t("not_found")}
      </div>
    );
  }
  return <AuctionClientCustom item={data} locale={locale} />;
}
