const generateHomepageMetaTags = (baseUrl, config) => {
  const description = config.descriptions.de.long;
  const keywords = config.keywords.de;
  const imageUrl = `${baseUrl}${config.images.logo}`;
  
  // Ensure URLs are properly formatted
  const canonicalUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;

  return `
    <!-- SEO Meta Tags -->
    <meta name="description" content="${description}">
    <meta name="keywords" content="${keywords}">
    
    <!-- Open Graph Meta Tags -->
    <meta property="og:title" content="${config.descriptions.de.short}">
    <meta property="og:description" content="${description}">
    <meta property="og:image" content="${imageUrl}">
    <meta property="og:url" content="${canonicalUrl}">
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="${config.siteName}">
    
    <!-- Twitter Card Meta Tags -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="${config.descriptions.de.short}">
    <meta name="twitter:description" content="${description}">
    <meta name="twitter:image" content="${imageUrl}">
    
    <!-- Additional Meta Tags -->
    <meta name="robots" content="index, follow">
    <link rel="canonical" href="${canonicalUrl}">
  `;
};

const generateHomepageJsonLd = (baseUrl, config, categories = []) => {
  // Ensure URLs are properly formatted
  const canonicalUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
  const logoUrl = `${canonicalUrl}${config.images.logo}`;

  const id = {
    business: `${canonicalUrl}#business`,
    website: `${canonicalUrl}#website`,
    faq: `${canonicalUrl}#faq`,
    categoryList: `${canonicalUrl}#category-list`,
    sitemapPage: `${canonicalUrl}/sitemap#webpage`,
  };

  const organizationNode = {
    "@id": id.business,
    "@type": ["GardenStore", "LocalBusiness", "Organization"],
    name: config.brandName,
    alternateName: config.siteName,
    description: config.descriptions.de.long,
    url: canonicalUrl,
    logo: {
      "@type": "ImageObject",
      url: logoUrl,
    },
    image: {
      "@type": "ImageObject",
      url: logoUrl,
    },
    telephone: "015208491860",
    email: "service@growheads.de",
    address: {
      "@type": "PostalAddress",
      streetAddress: "Trachenberger Strasse 14",
      addressLocality: "Dresden",
      postalCode: "01129",
      addressCountry: "DE",
      addressRegion: "Sachsen",
    },
    geo: {
      "@type": "GeoCoordinates",
      latitude: "51.083675",
      longitude: "13.727215",
    },
    openingHours: [
      "Mo-Fr 10:00:00-20:00:00",
      "Sa 11:00:00-19:00:00",
    ],
    paymentAccepted: "Cash, Credit Card, PayPal, Bank Transfer",
    currenciesAccepted: "EUR",
    priceRange: "€€",
    areaServed: {
      "@type": "Country",
      name: "Germany",
    },
    contactPoint: [
      {
        "@type": "ContactPoint",
        telephone: "015208491860",
        contactType: "customer service",
        availableLanguage: "German",
        hoursAvailable: {
          "@type": "OpeningHoursSpecification",
          dayOfWeek: [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
          ],
          opens: "10:00:00",
          closes: "20:00:00",
        },
      },
      {
        "@type": "ContactPoint",
        email: "service@growheads.de",
        contactType: "customer service",
        availableLanguage: "German",
      },
    ],
    sameAs: [],
  };

  const sitemapWebPageNode = {
    "@id": id.sitemapPage,
    "@type": "WebPage",
    name: "Sitemap",
    url: `${canonicalUrl}/sitemap`,
    description: "Vollständige Sitemap mit allen Kategorien und Seiten",
    isPartOf: { "@id": id.website },
  };

  const websiteNode = {
    "@id": id.website,
    "@type": "WebSite",
    name: config.brandName,
    url: canonicalUrl,
    description: config.descriptions.de.long,
    publisher: { "@id": id.business },
    potentialAction: {
      "@type": "SearchAction",
      target: `${canonicalUrl}/search?q={search_term_string}`,
      query: "required name=search_term_string",
    },
    mainEntity: { "@id": id.sitemapPage },
    sameAs: [],
  };

  const faqMainEntity = [
    {
      "@type": "Question",
      name: "Welche Zahlungsmethoden akzeptiert GrowHeads?",
      acceptedAnswer: {
        "@type": "Answer",
        text: "Wir akzeptieren Kreditkarten, PayPal, Banküberweisung, Nachnahme und Barzahlung bei Abholung in unserem Laden in Dresden.",
      },
    },
    {
      "@type": "Question",
      name: "Liefert GrowHeads deutschlandweit?",
      acceptedAnswer: {
        "@type": "Answer",
        text: "Ja, wir liefern deutschlandweit. Zusätzlich haben wir einen Laden in Dresden (Trachenberger Strasse 14) für lokale Kunden.",
      },
    },
    {
      "@type": "Question",
      name: "Welche Produkte bietet GrowHeads?",
      acceptedAnswer: {
        "@type": "Answer",
        text: "Wir bieten ein komplettes Sortiment für den Indoor-Anbau: Beleuchtung, Belüftung, Dünger, Töpfe, Zelte, Messgeräte und vieles mehr für professionelle Zuchtanlagen.",
      },
    },
    {
      "@type": "Question",
      name: "Hat GrowHeads einen physischen Laden?",
      acceptedAnswer: {
        "@type": "Answer",
        text: "Ja, unser Laden befindet sich in Dresden, Trachenberger Strasse 14. Öffnungszeiten: Mo-Fr 10-20 Uhr, Sa 11-19 Uhr. Sie können auch online bestellen.",
      },
    },
    {
      "@type": "Question",
      name: "Bietet GrowHeads Beratung zum Indoor-Anbau?",
      acceptedAnswer: {
        "@type": "Answer",
        text: "Ja, unser erfahrenes Team berät Sie gerne zu allen Aspekten des Indoor-Anbaus. Kontaktieren Sie uns telefonisch unter 015208491860 oder besuchen Sie unseren Laden.",
      },
    },
  ];

  const faqNode = {
    "@id": id.faq,
    "@type": "FAQPage",
    url: canonicalUrl,
    publisher: { "@id": id.business },
    isPartOf: { "@id": id.website },
    mainEntity: faqMainEntity,
  };

  const filteredCategories = categories.filter((c) => c.seoName);

  const graph = [
    organizationNode,
    websiteNode,
    sitemapWebPageNode,
    faqNode,
  ];

  if (filteredCategories.length > 0) {
    graph.push({
      "@id": id.categoryList,
      "@type": "ItemList",
      name: "Produktkategorien",
      description: "Alle verfügbaren Produktkategorien in unserem Online-Shop",
      numberOfItems: filteredCategories.length,
      isPartOf: { "@id": id.website },
      itemListElement: filteredCategories.map((category, index) => ({
        "@type": "ListItem",
        position: index + 1,
        item: {
          "@type": "Thing",
          name: category.name,
          url: `${canonicalUrl}/Kategorie/${category.seoName}`,
        },
      })),
    });
  }

  const homepageGraph = {
    "@context": "https://schema.org",
    "@graph": graph,
  };

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

module.exports = {
  generateHomepageMetaTags,
  generateHomepageJsonLd,
}; 