import type { SiteSettings, Service, Testimonial, FAQ } from "@prisma/client";

export function generateLocalBusinessSchema(
  settings: SiteSettings,
  services: Service[]
) {
  return {
    "@context": "https://schema.org",
    "@type": "LocalBusiness",
    name: settings.businessName,
    description: settings.metaDescription,
    url: settings.canonicalUrl || process.env.NEXT_PUBLIC_SITE_URL,
    telephone: settings.phone,
    email: settings.email,
    address: {
      "@type": "PostalAddress",
      streetAddress: settings.address,
    },
    image: settings.logoUrl || settings.heroImageUrl,
    priceRange: "$$",
    ...(settings.googleReviewRating && {
      aggregateRating: {
        "@type": "AggregateRating",
        ratingValue: settings.googleReviewRating,
        reviewCount: settings.googleReviewCount || "100",
      },
    }),
    hasOfferCatalog: {
      "@type": "OfferCatalog",
      name: "Services",
      itemListElement: services.map((service) => ({
        "@type": "Offer",
        itemOffered: {
          "@type": "Service",
          name: service.name,
          description: service.description,
        },
      })),
    },
  };
}

export function generateServiceSchema(settings: SiteSettings, services: Service[]) {
  return services.map((service) => ({
    "@context": "https://schema.org",
    "@type": "Service",
    name: service.name,
    description: service.description,
    provider: {
      "@type": "LocalBusiness",
      name: settings.businessName,
      telephone: settings.phone,
    },
  }));
}

export function generateFAQSchema(faqs: FAQ[]) {
  if (faqs.length === 0) return null;
  return {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: faqs.map((faq) => ({
      "@type": "Question",
      name: faq.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: faq.answer,
      },
    })),
  };
}

export function generateReviewSchema(testimonials: Testimonial[]) {
  if (testimonials.length === 0) return null;
  return testimonials.map((t) => ({
    "@context": "https://schema.org",
    "@type": "Review",
    author: {
      "@type": "Person",
      name: t.name,
    },
    reviewRating: {
      "@type": "Rating",
      ratingValue: t.rating,
      bestRating: 5,
    },
    reviewBody: t.review,
  }));
}
