import type { Metadata } from 'next';
import { config } from './config';
import { getBaseUrl } from './seo';

/**
 * Generate base metadata with Open Graph, Twitter Cards, and canonical URLs
 */
export async function generateBaseMetadata(
    locale: string,
    path: string = '',
    title?: string,
    description?: string,
    image?: string
): Promise<Metadata> {
    const baseUrl = await getBaseUrl();
    const pathSegment = path ? `/${path}` : '';
    const fullUrl = `${baseUrl}/${locale}${pathSegment}`;
    
    // Determine alternate URLs for both locales (same path for both)
    const alternateLocale = locale === 'de' ? 'en' : 'de';
    const alternateUrl = `${baseUrl}/${alternateLocale}${pathSegment}`;
    
    // Default values
    const metaTitle = title || config.appName;
    const metaDescription = description || config.appDescription;
    const ogImage = image || `${baseUrl}${config.seo.ogImage}`;
    
    return {
        title: metaTitle,
        description: metaDescription,
        alternates: {
            canonical: fullUrl,
            languages: {
                [locale]: fullUrl,
                [alternateLocale]: alternateUrl,
                'x-default': `${baseUrl}/en${pathSegment}`,
            },
        },
        openGraph: {
            title: metaTitle,
            description: metaDescription,
            url: fullUrl,
            siteName: config.appName,
            images: [
                {
                    url: ogImage,
                    width: 1200,
                    height: 630,
                    alt: metaTitle,
                },
            ],
            locale: locale,
            type: 'website',
        },
        twitter: {
            card: 'summary_large_image',
            title: metaTitle,
            description: metaDescription,
            images: [ogImage],
            ...(config.seo.twitterHandle && { creator: config.seo.twitterHandle }),
        },
    };
}

