import { setRequestLocale } from 'next-intl/server';
import type { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import { ContactForm } from '@/components/contact';
import { BreadcrumbJsonLd, WebPageJsonLd } from '@/components/seo';
import { type Locale } from '@/i18n/config';

type Props = {
  params: Promise<{ locale: string }>;
};

const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'https://damjan-savic.com';

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { locale } = await params;
  const t = await getTranslations({ locale, namespace: 'pages.contact' });

  const titles: Record<string, string> = {
    de: 'Kontakt - KI Entwickler Köln | Damjan Savić',
    en: 'Contact - Remote AI Developer | Hire for USA, UK & Europe Projects',
    sr: 'Kontakt - AI Developer Keln | Damjan Savić',
  };

  const descriptions: Record<string, string> = {
    de: 'Kontaktieren Sie Damjan Savić - Ihren AI & Automation Specialist aus Köln. Entwicklung von KI-Agenten, Voice AI, Prozessautomatisierung und SaaS-Lösungen.',
    en: 'Hire Damjan Savić - Remote AI Developer based in Germany. Available for clients in USA, UK & Europe. Specializing in AI agents, Voice AI, n8n automation and custom SaaS solutions. Free consultation.',
    sr: 'Kontaktirajte Damjana Savića - AI Developer i Automation Specialist iz Nemačke, srpskog porekla. Razvoj AI agenata, Voice AI, n8n automatizacija i SaaS rešenja za klijente iz Srbije i dijaspore.',
  };

  const keywords: Record<string, string[]> = {
    de: ['Kontakt Damjan Savić', 'KI Entwickler kontaktieren', 'Freelancer Köln', 'AI Beratung'],
    en: ['Hire AI Developer', 'Contact Remote Developer', 'AI Consultant USA', 'AI Developer UK', 'Freelance AI Developer', 'n8n Expert for Hire'],
    sr: ['Kontakt AI Developer', 'AI Programer Srbija', 'Freelance Programer Beograd', 'AI Konsultant Srbija', 'Softverski Razvoj Srbija', 'Remote Developer Dijaspora', 'n8n Automatizacija Srbija'],
  };

  const localePath = locale === 'de' ? '/contact' : `/${locale}/contact`;

  return {
    title: titles[locale] || titles.de,
    description: descriptions[locale] || descriptions.de,
    keywords: keywords[locale] || keywords.de,
    alternates: {
      canonical: `${BASE_URL}${localePath}`,
      languages: {
        'x-default': `${BASE_URL}/de/contact`,
        de: `${BASE_URL}/de/contact`,
        en: `${BASE_URL}/en/contact`,
        sr: `${BASE_URL}/sr/contact`,
      },
    },
    openGraph: {
      title: titles[locale] || titles.de,
      description: descriptions[locale] || descriptions.de,
      url: `${BASE_URL}${localePath}`,
      type: 'website',
      locale: locale === 'de' ? 'de_DE' : locale === 'sr' ? 'sr_RS' : 'en_US',
      alternateLocale: ['de_DE', 'en_US', 'sr_RS'].filter(l => l !== (locale === 'de' ? 'de_DE' : locale === 'sr' ? 'sr_RS' : 'en_US')),
    },
  };
}

export default async function ContactPage({ params }: Props) {
  const { locale } = await params;
  setRequestLocale(locale);

  const t = await getTranslations({ locale, namespace: 'pages.contact' });
  const commonT = await getTranslations({ locale, namespace: 'common.nav' });

  const breadcrumbItems = [
    { name: commonT('home'), url: `/${locale}` },
    { name: commonT('contact'), url: `/${locale}/contact` },
  ];

  return (
    <>
      <BreadcrumbJsonLd items={breadcrumbItems} locale={locale as Locale} />
      <WebPageJsonLd
        title={t('hero.title')}
        description={t('hero.subtitle')}
        url={`/${locale}/contact`}
        locale={locale as Locale}
      />
      <ContactForm />
    </>
  );
}
