import Head from 'next/head';
import { getTranslations, type Language } from '@/lib/languages';

interface SEOHeadProps {
    lang: Language;
    page: 'home' | 'products' | 'security' | 'datacenter' | 'support' | 'console';
    customTitle?: string;
    customDescription?: string;
    customKeywords?: string;
}

export default function SEOHead({
    lang,
    page,
    customTitle,
    customDescription,
    customKeywords,
}: SEOHeadProps) {
    const t = getTranslations(lang);
    const seoData = t.seo[page];

    const title = customTitle || seoData.title;
    const description = customDescription || seoData.description;
    const keywords = customKeywords || seoData.keywords;

    const canonicalUrl = `https://cybercloud.com/${lang}${page === 'home' ? '' : `/${page}`}`;
    const alternateUrls = {
        zh: `https://cybercloud.com/zh${page === 'home' ? '' : `/${page}`}`,
        tw: `https://cybercloud.com/tw${page === 'home' ? '' : `/${page}`}`,
        en: `https://cybercloud.com/en${page === 'home' ? '' : `/${page}`}`,
    };

    return (
        <Head>
            {/* Basic Meta Tags */}
            <title>{title}</title>
            <meta name="description" content={description} />
            <meta name="keywords" content={keywords} />
            <meta name="author" content="CyberCloud" />
            <meta name="robots" content="index, follow" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />

            {/* Language and Locale */}
            <meta httpEquiv="content-language" content={lang} />
            <meta name="language" content={lang} />

            {/* Canonical URL */}
            <link rel="canonical" href={canonicalUrl} />

            {/* Alternate Language URLs */}
            <link rel="alternate" hrefLang="zh" href={alternateUrls.zh} />
            <link rel="alternate" hrefLang="zh-CN" href={alternateUrls.zh} />
            <link rel="alternate" hrefLang="zh-TW" href={alternateUrls.tw} />
            <link rel="alternate" hrefLang="zh-HK" href={alternateUrls.tw} />
            <link rel="alternate" hrefLang="en" href={alternateUrls.en} />
            <link rel="alternate" hrefLang="x-default" href={alternateUrls.zh} />

            {/* Open Graph Meta Tags */}
            <meta property="og:type" content="website" />
            <meta property="og:title" content={title} />
            <meta property="og:description" content={description} />
            <meta property="og:url" content={canonicalUrl} />
            <meta property="og:site_name" content="CyberCloud" />
            <meta
                property="og:locale"
                content={lang === 'zh' ? 'zh_CN' : lang === 'tw' ? 'zh_TW' : 'en_US'}
            />

            <meta property="og:image" content="https://cybercloud.com/images/og-image.jpg" />

            <meta property="og:image:width" content="1200" />
            <meta property="og:image:height" content="630" />
            <meta property="og:image:alt" content="CyberCloud - 云端未来" />

            {/* Twitter Card Meta Tags */}
            <meta name="twitter:card" content="summary_large_image" />
            <meta name="twitter:site" content="@CyberCloud" />
            <meta name="twitter:creator" content="@CyberCloud" />
            <meta name="twitter:title" content={title} />
            <meta name="twitter:description" content={description} />
            <meta name="twitter:image" content="https://cybercloud.com/images/twitter-card.jpg" />

            {/* Additional SEO Meta Tags */}
            <meta name="theme-color" content="#00ffff" />
            <meta name="msapplication-TileColor" content="#000000" />
            <meta name="application-name" content="CyberCloud" />

            {/* Structured Data - Organization */}
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify({
                        '@context': 'https://schema.org',
                        '@type': 'Organization',
                        name: 'CyberCloud',
                        url: 'https://cybercloud.com',
                        logo: 'https://cybercloud.com/images/logo.png',
                        description: description,
                        contactPoint: {
                            '@type': 'ContactPoint',
                            telephone: '+86-400-123-4567',
                            contactType: 'customer service',
                            availableLanguage: ['Chinese', 'English'],
                        },
                        sameAs: [
                            'https://twitter.com/cybercloud',
                            'https://linkedin.com/company/cybercloud',
                            'https://github.com/cybercloud',
                        ],
                    }),
                }}
            />

            {/* Structured Data - WebSite */}
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify({
                        '@context': 'https://schema.org',
                        '@type': 'WebSite',
                        name: 'CyberCloud',
                        url: 'https://cybercloud.com',
                        description: description,
                        potentialAction: {
                            '@type': 'SearchAction',
                            target: 'https://cybercloud.com/search?q={search_term_string}',
                            'query-input': 'required name=search_term_string',
                        },
                    }),
                }}
            />

            {/* Page-specific Structured Data */}
            {page === 'products' && (
                <script
                    type="application/ld+json"
                    dangerouslySetInnerHTML={{
                        __html: JSON.stringify({
                            '@context': 'https://schema.org',
                            '@type': 'Product',
                            name: 'CyberCloud 云服务器',
                            description: '高性能、可扩展的云服务器解决方案',
                            brand: {
                                '@type': 'Brand',
                                name: 'CyberCloud',
                            },
                            offers: {
                                '@type': 'Offer',
                                availability: 'https://schema.org/InStock',
                                priceCurrency: 'CNY',
                            },
                        }),
                    }}
                />
            )}

            {page === 'datacenter' && (
                <script
                    type="application/ld+json"
                    dangerouslySetInnerHTML={{
                        __html: JSON.stringify({
                            '@context': 'https://schema.org',
                            '@type': 'Place',
                            name: 'CyberCloud 全球数据中心',
                            description: '遍布全球的高可用数据中心网络',
                            geo: {
                                '@type': 'GeoCoordinates',
                                latitude: '31.2304',
                                longitude: '121.4737',
                            },
                        }),
                    }}
                />
            )}
        </Head>
    );
}
