import type { Metadata } from 'next';

const APP_URL = process.env.NEXT_PUBLIC_APP_URL || 'https://indianbooksworldwide.com';
const APP_NAME = 'Indian Books Worldwide';

export interface SeoConfig {
  title: string;
  description: string;
  path?: string;
  image?: string;
  keywords?: string[];
  type?: 'website' | 'article' | 'book';
}

export function generateSeoMetadata({
  title,
  description,
  path = '',
  image = 'https://images.unsplash.com/photo-1544716278-ca5e3f4abd8c?w=1200&auto=format&fit=crop&q=80',
  keywords = ['Indian Books', 'Vedic Scriptures', 'Sanskrit Manuscripts', 'Rabindranath Tagore', 'Indian Literature'],
  type = 'website',
}: SeoConfig): Metadata {
  const url = `${APP_URL}${path}`;

  return {
    title: `${title} | ${APP_NAME}`,
    description,
    keywords: keywords.join(', '),
    metadataBase: new URL(APP_URL),
    alternates: {
      canonical: url,
    },
    openGraph: {
      title,
      description,
      url,
      siteName: APP_NAME,
      images: [
        {
          url: image,
          width: 1200,
          height: 630,
          alt: title,
        },
      ],
      type: type as any,
      locale: 'en_IN',
    },
    twitter: {
      card: 'summary_large_image',
      title,
      description,
      images: [image],
      creator: '@indianbooks',
    },
    robots: {
      index: true,
      follow: true,
      googleBot: {
        index: true,
        follow: true,
        'max-video-preview': -1,
        'max-image-preview': 'large',
        'max-snippet': -1,
      },
    },
  };
}

// Schema.org Structured Data Generators
export function generateBookSchema(book: {
  title: string;
  isbn: string;
  description: string;
  price: number;
  authorName?: string;
  publisherName?: string;
  coverImage?: string;
  ratingAverage?: number;
  ratingCount?: number;
}) {
  return {
    '@context': 'https://schema.org',
    '@type': 'Book',
    name: book.title,
    isbn: book.isbn,
    description: book.description,
    image: book.coverImage,
    author: {
      '@type': 'Person',
      name: book.authorName || 'Indian Author',
    },
    publisher: {
      '@type': 'Organization',
      name: book.publisherName || 'Indian Books Worldwide Press',
    },
    offers: {
      '@type': 'Offer',
      priceCurrency: 'INR',
      price: book.price,
      availability: 'https://schema.org/InStock',
      url: `${APP_URL}/books/${book.isbn}`,
    },
    aggregateRating: book.ratingAverage
      ? {
          '@type': 'AggregateRating',
          ratingValue: book.ratingAverage,
          reviewCount: book.ratingCount || 1,
        }
      : undefined,
  };
}

export function generateOrganizationSchema() {
  return {
    '@context': 'https://schema.org',
    '@type': 'OnlineStore',
    name: APP_NAME,
    url: APP_URL,
    logo: `${APP_URL}/logo.png`,
    sameAs: [
      'https://twitter.com/indianbooks',
      'https://facebook.com/indianbooksworldwide',
      'https://instagram.com/indianbooks',
    ],
    contactPoint: {
      '@type': 'ContactPoint',
      telephone: '+91-11-2345-6789',
      contactType: 'customer service',
      areaServed: 'Worldwide',
      availableLanguage: ['English', 'Hindi'],
    },
  };
}
