import React from 'react';
import type { Metadata } from 'next';
import SeoJsonLd from '@/components/SeoJsonLd';
import {
  websiteSchema,
  softwareApplicationSchema,
} from '@/lib/schema';
import { getAggregateRating } from '@/lib/testimonial-data';
import HomePageClient from '@/components/marketing/HomePageClient';

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 description = truncateAtWord(
    'Change where a printed QR code points - without reprinting it. 3 dynamic codes free forever, unlimited static codes that never expire, scan tracking. No card.',
    160
  );
  const brandTitle = 'QR Master - Free QR Code Generator, Editable After Print';

  return {
    title: brandTitle,
    description,
    keywords: [
      'qr generator',
      'free qr code generator',
      'custom qr code generator',
      'qr code maker',
      'online qr code generator',
      'dynamic qr code',
      'qr code with logo',
      'barcode generator',
      'free barcode generator',
      'qr master',
      'qrmaster',
      'qr code master',
    ],
    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: brandTitle,
      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: brandTitle,
      description,
    },
  };
}

export default function HomePage() {
  const aggregateRating = getAggregateRating();

  return (
    <>
      <SeoJsonLd
        data={[
          websiteSchema(),
          softwareApplicationSchema(aggregateRating),
        ]}
      />

      {/* 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, free colour control on every plan,
          advanced scan analytics showing device types and locations, vCard QR
          codes for digital business cards, restaurant menu QR codes, and a free{' '}
          <a href="/tools/barcode-generator">barcode generator</a> for EAN-13,
          UPC-A, and Code 128 barcodes.
        </p>
        <p>
          Popular QR Master workflows include the{' '}
          <a href="/dynamic-qr-code-generator">
            free dynamic QR code generator
          </a>
          , <a href="/qr-code-tracking">QR code tracking</a>, and the{' '}
          <a href="/custom-qr-code-generator">custom QR code generator</a> for
          branded print campaigns.
        </p>
        <p>
          Start free with 3 active 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>
        <p>
          Frequently used QR Master tools and industry workflows include the{' '}
          <a href="/tools/teams-qr-code">Teams QR code generator</a>,{' '}
          <a href="/tools/wifi-qr-code">WiFi QR code generator</a>,{' '}
          <a href="/qr-code-erstellen">German QR code generator</a>, and{' '}
          <a href="/qr-code-for/barbershops">QR codes for barbershops</a>.
        </p>
        <p>
          High-intent QR Master pages include the{' '}
          <a href="/bulk-qr-code-generator">
            bulk QR code generator for Excel and CSV files
          </a>
          , <a href="/alternatives">QR code platform alternatives</a>,{' '}
          <a href="/alternatives/beaconstac">Beaconstac alternative</a>, and{' '}
          <a href="/alternatives/bitly">Bitly QR code alternative</a>.
        </p>
      </div>

      <HomePageClient />
    </>
  );
}
