import {
  hreflangMap,
  localizedPath,
  locales,
  type Locale,
  type RouteId,
} from '@/lib/localization/config';
import { getSiteOrigin } from '@/lib/localization/site-origin';
import type { Metadata } from 'next';

interface LocalizedMetadataInput {
  locale: Locale;
  routeId: RouteId;
  title: string;
  description: string;
  indexable: boolean;
}

export function buildLocalizedMetadata({
  locale,
  routeId,
  title,
  description,
  indexable,
}: LocalizedMetadataInput): Metadata {
  const origin = getSiteOrigin();
  const publicReleaseApproved = process.env.PUBLIC_RELEASE_APPROVED === 'true';
  const canIndex = publicReleaseApproved && indexable;
  const canonicalPath = localizedPath(routeId, locale);

  return {
    metadataBase: origin,
    title,
    description,
    alternates: {
      canonical: canonicalPath,
      languages: hreflangMap(origin, routeId),
    },
    openGraph: {
      type: 'website',
      siteName: 'RentalDriveGo',
      title,
      description,
      url: canonicalPath,
      locale,
      alternateLocale: locales.filter((candidate) => candidate !== locale),
    },
    twitter: {
      card: 'summary',
      title,
      description,
    },
    robots: canIndex
      ? { index: true, follow: true }
      : { index: false, follow: false, noarchive: true },
  };
}
