import React from 'react';
import type { Metadata } from 'next';
import SeoJsonLd from '@/components/SeoJsonLd';
import { organizationSchema, websiteSchema } from '@/lib/schema';
import HomePageClient from '@/components/marketing/HomePageClient';
import { generateFaqSchema } from '@/lib/schema-utils';
import en from '@/i18n/en.json'; // Import English translations for schema generation

function truncateAtWord(text: string, maxLength: number): string {
  if (text.length <= maxLength) return text;
  const truncated = text.slice(0, maxLength);
  const lastSpace = truncated.lastIndexOf(' ');
  return lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated;
}

export async function generateMetadata(): Promise<Metadata> {
  const title = truncateAtWord('QR Master: Dynamic QR Generator', 60);
  const description = truncateAtWord(
    'Create professional QR codes with QR Master. Dynamic QR with tracking, bulk generation, custom branding, and real-time analytics for all your campaigns.',
    160
  );

  return {
    title,
    description,
    alternates: {
      canonical: 'https://www.qrmaster.net/',
      languages: {
        'x-default': 'https://www.qrmaster.net/',
        en: 'https://www.qrmaster.net/',
        de: 'https://www.qrmaster.net/qr-code-erstellen',
      },
    },
    openGraph: {
      title,
      description,
      url: 'https://www.qrmaster.net/',
      type: 'website',
      images: [
        {
          url: 'https://www.qrmaster.net/og-image.png',
          width: 1200,
          height: 630,
          alt: 'QR Master - Dynamic QR Code Generator and Analytics Platform',
        },
      ],
    },
    twitter: {
      title,
      description,
      images: ['https://www.qrmaster.net/og-image.png'],
    },
  };
}

export default function HomePage() {
  return (
    <>
      <SeoJsonLd data={[organizationSchema(), websiteSchema(), generateFaqSchema(en.faq.questions)]} />

      {/* Server-rendered H1 for SEO - visually hidden but crawlable */}
      <h1 className="sr-only">QR Master: Dynamic QR Code Generator with Analytics</h1>

      {/* Server-rendered SEO content for crawlers */}
      <div className="sr-only" aria-hidden="false">
        <p>
          Create professional QR codes for your business with QR Master. Our dynamic QR code generator
          lets you create trackable QR codes, edit destinations anytime, and view detailed analytics.
          Perfect for restaurants, retail, events, and marketing campaigns.
        </p>
        <p>
          Features include: Dynamic QR codes with real-time tracking, bulk QR code generation from Excel/CSV,
          custom branding with colors and logos, advanced scan analytics showing device types and locations,
          vCard QR codes for digital business cards, and restaurant menu QR codes.
        </p>
        <p>
          Start free with 3 dynamic QR codes and unlimited static codes. Upgrade to Pro for 50 codes
          with advanced analytics, or Business for 500 codes with bulk creation and priority support.
        </p>
      </div>

      <HomePageClient />
    </>
  );
}
