import { Project } from "@/types/types";
import { DEFAULT_LANGUAGE, type AppLanguage } from "@/lib/i18n/registry";
import { formatLocalizedSeoTitle, getSiteSeoMeta } from "@/lib/i18n/seo";
import { translateCommon } from "@/lib/i18n/translate";

export function formatProjectBudget(
  price: number | null | undefined,
  lang: AppLanguage = DEFAULT_LANGUAGE
): string {
  if (price == null || Number.isNaN(Number(price))) return "";
  const locale = lang === "fa" ? "fa-IR" : "en-US";
  return translateCommon(lang, "courseSeo.priceToman", {
    price: Number(price).toLocaleString(locale),
  });
}

export function buildProjectDetailSeo(
  project: Project,
  lang: AppLanguage = DEFAULT_LANGUAGE
) {
  const siteName = getSiteSeoMeta(lang).name;
  const titleCore = translateCommon(lang, "projectSeo.registeredInExpertise", {
    title: project.title,
    expertise: project.expertise,
  });
  const budget = formatProjectBudget(project.offer_price, lang);
  const description = [budget, project.description?.trim()]
    .filter(Boolean)
    .join(" – ");

  const path = `/projects/${project._id}/${encodeURIComponent(project.title)}`;

  return {
    title: formatLocalizedSeoTitle(titleCore, lang),
    description:
      description ||
      translateCommon(lang, "projectSeo.detailDescription", {
        title: project.title,
        siteName,
      }),
    path,
  };
}

export function buildProjectDetailFallbackSeo(
  title: string,
  id: string,
  lang: AppLanguage = DEFAULT_LANGUAGE
) {
  const siteName = getSiteSeoMeta(lang).name;
  const decodedTitle =
    title ||
    translateCommon(lang, "projectSeo.fallbackProject");

  return {
    title: formatLocalizedSeoTitle(decodedTitle, lang),
    description: translateCommon(lang, "projectSeo.detailDescription", {
      title: decodedTitle,
      siteName,
    }),
    path: `/projects/${id}/${encodeURIComponent(decodedTitle)}`,
  };
}
