import type { Metadata } from 'next';
import type { Locale } from '@/i18n/config';
import { siteConfig } from '@/data/site-config';

const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000';

interface BuildMetadataArgs {
  locale: Locale;
  path: string; // e.g. "/services/installation-residentielle"
  title: string;
  description: string;
  image?: string;
  type?: 'website' | 'article' | 'product';
}

export function buildMetadata({
  locale,
  path,
  title,
  description,
  image,
  type = 'website'
}: BuildMetadataArgs): Metadata {
  const url = `${SITE_URL}/${locale}${path}`;
  const alternateLocale = locale === 'fr' ? 'ar' : 'fr';

  return {
    title: `${title} | ${siteConfig.companyName}`,
    description,
    alternates: {
      canonical: url,
      languages: {
        fr: `${SITE_URL}/fr${path}`,
        ar: `${SITE_URL}/ar${path}`
      }
    },
    openGraph: {
      title,
      description,
      url,
      siteName: siteConfig.companyName,
      locale,
      alternateLocale,
      type: type === 'product' ? 'website' : type,
      images: image ? [{ url: image }] : undefined
    },
    twitter: {
      card: 'summary_large_image',
      title,
      description,
      images: image ? [image] : undefined
    }
  };
}
