import type { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import { routing, type Locale } from '@/i18n/routing';
import { site } from '@/content/site';

const OG_LOCALE: Record<Locale, string> = { ar: 'ar_EG', en: 'en_US' };

export async function buildMetadata(locale: Locale): Promise<Metadata> {
  const t = await getTranslations({ locale, namespace: 'meta' });
  const title = t('title');
  const description = t('description', { project: site.project });
  const url = `${site.url}/${locale}`;
  const image = { url: '/images/og.jpg', width: 1200, height: 630, alt: t('ogAlt') };

  return {
    metadataBase: new URL(site.url),
    title,
    description,
    applicationName: site.name,
    alternates: {
      canonical: url,
      languages: {
        ...Object.fromEntries(routing.locales.map((l) => [l, `${site.url}/${l}`])),
        'x-default': `${site.url}/${routing.defaultLocale}`,
      },
    },
    openGraph: {
      type: 'website',
      siteName: site.name,
      title,
      description,
      url,
      locale: OG_LOCALE[locale],
      alternateLocale: routing.locales.filter((l) => l !== locale).map((l) => OG_LOCALE[l]),
      images: [image],
    },
    twitter: { card: 'summary_large_image', title, description, images: [image] },
    robots: { index: site.indexable, follow: site.indexable },
  };
}

/** Organization + WebSite only — no reviews, ratings or offers, since none are supplied. */
export function buildJsonLd(locale: Locale) {
  return {
    '@context': 'https://schema.org',
    '@graph': [
      {
        '@type': 'Organization',
        '@id': `${site.url}/#organization`,
        name: site.name,
        url: site.url,
        logo: `${site.url}/images/logo.png`,
      },
      {
        '@type': 'WebSite',
        '@id': `${site.url}/#website`,
        url: `${site.url}/${locale}`,
        name: site.name,
        inLanguage: locale,
        publisher: { '@id': `${site.url}/#organization` },
      },
    ],
  };
}
