import type { Metadata } from 'next';

interface SEOInput {
    title?: string;
    description?: string;
    image?: string;
    keywords?: string;
    fallbackTitle?: string;
}

export function buildMetadata({
    title,
    description,
    image,
    keywords,
    fallbackTitle,
}: SEOInput): Metadata {
    const finalTitle = title || fallbackTitle;

    // ✅ Convert comma-separated keywords → array
    const formattedKeywords = keywords
        ? keywords
            .split(',')
            .map((k) => k.trim())
            .filter(Boolean)
        : undefined;

    return {
        title: finalTitle,
        description: description,
        keywords: formattedKeywords,

        openGraph: {
            title: finalTitle,
            description: description,
            images: image ? [image] : undefined,
        },

        twitter: {
            card: 'summary_large_image',
            title: finalTitle,
            description: description,
            images: image ? [image] : undefined,
        },
    };
}