'use client';

import { Locale, getTranslations } from '@/lib/i18n';
import { getSEOData } from '@/lib/seo';
import { useEffect, useState } from 'react';
import type { SEOData } from '@/lib/seo';

interface HomePageSEOProps {
    locale: Locale;
}

export default function HomePageSEO({ locale }: HomePageSEOProps) {
    const [seoData, setSeoData] = useState<SEOData | null>(null);
    const [homeData, setHomeData] = useState<any | null>(null);

    useEffect(() => {
        const loadData = async () => {
            try {
                const [seo, home] = await Promise.all([
                    getSEOData(locale, 'home'),
                    getTranslations(locale, 'home'),
                ]);
                setSeoData(seo);
                setHomeData(home);
            } catch (error) {
                console.error('Failed to load SEO data:', error);
            }
        };

        loadData();
    }, [locale]);

    const getLocalizedContent = () => {
        const content = {
            'zh-CN': {
                companyName: '创新科技',
                description: '专注企业数字化转型的科技公司',
                services: ['数字化转型咨询', 'ERP系统开发', 'AI数据分析', '云迁移服务', '网络安全'],
                address: '中国北京市朝阳区',
                phone: '+86-400-123-4567',
                email: 'contact@xintech',
            },
            'zh-TW': {
                companyName: '創新科技',
                description: '專注企業數位化轉型的科技公司',
                services: [
                    '數位化轉型諮詢',
                    'ERP系統開發',
                    'AI數據分析',
                    '雲端遷移服務',
                    '網路安全',
                ],

                address: '中國北京市朝陽區',
                phone: '+86-400-123-4567',
                email: 'contact@xintech',
            },
            en: {
                companyName: 'Innovation Tech',
                description: 'Technology company focused on enterprise digital transformation',
                services: [
                    'Digital Transformation Consulting',
                    'ERP System Development',
                    'AI Data Analytics',
                    'Cloud Migration Services',
                    'Cybersecurity',
                ],

                address: 'Chaoyang District, Beijing, China',
                phone: '+86-400-123-4567',
                email: 'contact@xintech',
            },
        };
        return content[locale] || content['zh-CN'];
    };

    const content = getLocalizedContent();
    const baseUrl = 'https://xintech';
    const currentUrl = `${baseUrl}${locale === 'zh-CN' ? '' : `/${locale}`}`;

    // Organization Schema
    const organizationSchema = {
        '@context': 'https://schema.org',
        '@type': 'Organization',
        '@id': `${baseUrl}/#organization`,
        name: content.companyName,
        alternateName: 'Innovation Tech',
        description: content.description,
        url: currentUrl,
        logo: {
            '@type': 'ImageObject',
            url: `${baseUrl}/logo.png`,
            width: 300,
            height: 100,
        },
        image: `${baseUrl}/company-image.jpg`,
        telephone: content.phone,
        email: content.email,
        address: {
            '@type': 'PostalAddress',
            addressCountry: 'CN',
            addressLocality: 'Beijing',
            addressRegion: 'Beijing',
            streetAddress: content.address,
        },
        contactPoint: [
            {
                '@type': 'ContactPoint',
                telephone: content.phone,
                contactType: 'customer service',
                availableLanguage: ['Chinese', 'English'],
                areaServed: ['CN', 'US', 'GB', 'AU'],
            },
            {
                '@type': 'ContactPoint',
                email: content.email,
                contactType: 'sales',
                availableLanguage: ['Chinese', 'English'],
            },
        ],

        sameAs: [
            'https://www.linkedin.com/company/haoaws',
            'https://twitter.com/innovation_tech',
            'https://www.facebook.com/innovation.tech',
            'https://github.com/haoaws',
        ],

        foundingDate: '2010-01-01',
        numberOfEmployees: {
            '@type': 'QuantitativeValue',
            value: 150,
        },
        slogan:
            locale === 'en'
                ? 'Leading Digital Transformation'
                : locale === 'zh-TW'
                  ? '引領數位化轉型'
                  : '引领数字化转型',
    };

    // Website Schema
    const websiteSchema = {
        '@context': 'https://schema.org',
        '@type': 'WebSite',
        '@id': `${baseUrl}/#website`,
        name: content.companyName,
        url: currentUrl,
        publisher: {
            '@id': `${baseUrl}/#organization`,
        },
        potentialAction: {
            '@type': 'SearchAction',
            target: {
                '@type': 'EntryPoint',
                urlTemplate: `${currentUrl}/search?q={search_term_string}`,
            },
            'query-input': 'required name=search_term_string',
        },
        inLanguage: locale === 'zh-CN' ? 'zh-CN' : locale === 'zh-TW' ? 'zh-TW' : 'en-US',
    };

    // Service Schema
    const serviceSchema = {
        '@context': 'https://schema.org',
        '@type': 'Service',
        name:
            locale === 'en'
                ? 'Digital Transformation Services'
                : locale === 'zh-TW'
                  ? '數位化轉型服務'
                  : '数字化转型服务',
        description: content.description,
        provider: {
            '@id': `${baseUrl}/#organization`,
        },
        serviceType: content.services,
        areaServed: {
            '@type': 'Country',
            name: 'China',
        },
        hasOfferCatalog: {
            '@type': 'OfferCatalog',
            name:
                locale === 'en'
                    ? 'Digital Solutions'
                    : locale === 'zh-TW'
                      ? '數位解決方案'
                      : '数字解决方案',
            itemListElement: content.services.map((service, index) => ({
                '@type': 'Offer',
                itemOffered: {
                    '@type': 'Service',
                    name: service,
                },
            })),
        },
    };

    // Local Business Schema
    const localBusinessSchema = {
        '@context': 'https://schema.org',
        '@type': 'ProfessionalService',
        '@id': `${baseUrl}/#localbusiness`,
        name: content.companyName,
        description: content.description,
        url: currentUrl,
        telephone: content.phone,
        email: content.email,
        address: {
            '@type': 'PostalAddress',
            addressCountry: 'CN',
            addressLocality: 'Beijing',
            streetAddress: content.address,
        },
        geo: {
            '@type': 'GeoCoordinates',
            latitude: '39.9042',
            longitude: '116.4074',
        },
        openingHours: 'Mo-Fr 09:00-18:00',
        priceRange: '$',
        aggregateRating: {
            '@type': 'AggregateRating',
            ratingValue: '4.8',
            reviewCount: '150',
            bestRating: '5',
            worstRating: '1',
        },
    };

    // FAQ Schema for common questions
    const faqSchema = {
        '@context': 'https://schema.org',
        '@type': 'FAQPage',
        mainEntity: [
            {
                '@type': 'Question',
                name:
                    locale === 'en'
                        ? 'What is digital transformation?'
                        : locale === 'zh-TW'
                          ? '什麼是數位化轉型？'
                          : '什么是数字化转型？',
                acceptedAnswer: {
                    '@type': 'Answer',
                    text:
                        locale === 'en'
                            ? 'Digital transformation is the integration of digital technology into all areas of a business, fundamentally changing how you operate and deliver value to customers.'
                            : locale === 'zh-TW'
                              ? '數位化轉型是將數位技術整合到企業的各個領域，從根本上改變企業的營運方式和為客戶提供價值的方式。'
                              : '数字化转型是将数字技术整合到企业的各个领域，从根本上改变企业的运营方式和为客户提供价值的方式。',
                },
            },
            {
                '@type': 'Question',
                name:
                    locale === 'en'
                        ? 'How long does digital transformation take?'
                        : locale === 'zh-TW'
                          ? '數位化轉型需要多長時間？'
                          : '数字化转型需要多长时间？',
                acceptedAnswer: {
                    '@type': 'Answer',
                    text:
                        locale === 'en'
                            ? 'Digital transformation timeline varies by company size and complexity, typically ranging from 6 months to 2 years for comprehensive transformation.'
                            : locale === 'zh-TW'
                              ? '數位化轉型的時間因公司規模和複雜性而異，全面轉型通常需要6個月到2年的時間。'
                              : '数字化转型的时间因公司规模和复杂性而异，全面转型通常需要6个月到2年的时间。',
                },
            },
        ],
    };

    return (
        <>
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify(organizationSchema),
                }}
            />

            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify(websiteSchema),
                }}
            />

            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify(serviceSchema),
                }}
            />

            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify(localBusinessSchema),
                }}
            />

            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{
                    __html: JSON.stringify(faqSchema),
                }}
            />
        </>
    );
}
