import { Helmet } from 'react-helmet-async';

interface ListingSchemaProps {
  listing: {
    id: number;
    title: string;
    description: string;
    price: number;
    currency?: string;
    category: string;
    images?: string[];
    seller?: {
      name: string;
      telephone?: string;
    };
    location?: string;
    datePosted?: string;
  };
}

// Schema.org markup para avisos clasificados (Product/Offer)
export function ListingSchema({ listing }: ListingSchemaProps) {
  const schema = {
    '@context': 'https://schema.org',
    '@type': 'Product',
    name: listing.title,
    description: listing.description,
    category: listing.category,
    image: listing.images || [],
    offers: {
      '@type': 'Offer',
      price: listing.price,
      priceCurrency: listing.currency || 'ARS',
      availability: 'https://schema.org/InStock',
      url: `${window.location.origin}/avisos/${listing.id}`,
      seller: listing.seller ? {
        '@type': 'Organization',
        name: listing.seller.name,
        telephone: listing.seller.telephone
      } : undefined
    },
    ...(listing.datePosted && { datePublished: listing.datePosted }),
    ...(listing.location && {
      availableAtOrFrom: {
        '@type': 'Place',
        address: {
          '@type': 'PostalAddress',
          addressLocality: listing.location
        }
      }
    })
  };

  return (
    <Helmet>
      <script type="application/ld+json">
        {JSON.stringify(schema)}
      </script>
    </Helmet>
  );
}

// Schema.org para la organización (footer del sitio)
export function OrganizationSchema() {
  const schema = {
    '@context': 'https://schema.org',
    '@type': 'Organization',
    name: 'Diario El Día',
    url: window.location.origin,
    logo: `${window.location.origin}/logo.png`,
    sameAs: [
      'https://www.facebook.com/diarioeldia',
      'https://twitter.com/diarioeldia',
      'https://www.instagram.com/diarioeldia'
    ],
    contactPoint: {
      '@type': 'ContactPoint',
      contactType: 'customer service',
      telephone: '+54-11-1234-5678',
      availableLanguage: 'Spanish'
    }
  };

  return (
    <Helmet>
      <script type="application/ld+json">
        {JSON.stringify(schema)}
      </script>
    </Helmet>
  );
}

// Schema.org para breadcrumbs de navegación
export function BreadcrumbSchema({ items }: { items: Array<{ name: string; url: string }> }) {
  const schema = {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement: items.map((item, index) => ({
      '@type': 'ListItem',
      position: index + 1,
      name: item.name,
      item: item.url
    }))
  };

  return (
    <Helmet>
      <script type="application/ld+json">
        {JSON.stringify(schema)}
      </script>
    </Helmet>
  );
}

// Schema.org para formulario de búsqueda
export function WebsiteSearchSchema() {
  const schema = {
    '@context': 'https://schema.org',
    '@type': 'WebSite',
    url: window.location.origin,
    potentialAction: {
      '@type': 'SearchAction',
      target: {
        '@type': 'EntryPoint',
        urlTemplate: `${window.location.origin}/buscar?q={search_term_string}`
      },
      'query-input': 'required name=search_term_string'
    }
  };

  return (
    <Helmet>
      <script type="application/ld+json">
        {JSON.stringify(schema)}
      </script>
    </Helmet>
  );
}
