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';

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(
    'Dynamic QR, branding, bulk generation & analytics for all campaigns.',
    160
  );

  return {
    title,
    description,
    alternates: {
      canonical: 'https://www.qrmaster.net/',
      languages: {
        'x-default': 'https://www.qrmaster.net/',
        en: 'https://www.qrmaster.net/',
      },
    },
    openGraph: {
      title,
      description,
      url: 'https://www.qrmaster.net/',
      type: 'website',
    },
    twitter: {
      title,
      description,
    },
  };
}

export default function HomePage() {
  return (
    <>
      <SeoJsonLd data={[organizationSchema(), websiteSchema()]} />
      <HomePageClient />
    </>
  );
}
