import { APP_NAME, SITE_URL, WHATSAPP_PHONE } from "@/utils/constants";
import type { SeoFaqItem, SeoPageContent, SeoPageContext } from "./types";

export function buildJsonLd(
  ctx: SeoPageContext,
  content: SeoPageContent,
): Record<string, unknown>[] {
  const pageUrl = `${SITE_URL}${content.canonicalPath}`;
  const schemas: Record<string, unknown>[] = [];

  schemas.push({
    "@context": "https://schema.org",
    "@type": "Organization",
    name: APP_NAME,
    url: SITE_URL,
    logo: `${SITE_URL}/assets/logo_nogacode_bc.png`,
    contactPoint: {
      "@type": "ContactPoint",
      telephone: `+${WHATSAPP_PHONE}`,
      contactType: "sales",
      areaServed: "BR",
      availableLanguage: "Portuguese",
    },
  });

  const localBusiness: Record<string, unknown> = {
    "@context": "https://schema.org",
    "@type": "LocalBusiness",
    name: APP_NAME,
    url: SITE_URL,
    description: content.metaDescription,
    areaServed: ctx.city
      ? { "@type": "City", name: ctx.city.name }
      : ctx.state
        ? { "@type": "State", name: ctx.state.name }
        : { "@type": "Country", name: "Brasil" },
    priceRange: "$$",
  };
  schemas.push(localBusiness);

  if (ctx.service) {
    schemas.push({
      "@context": "https://schema.org",
      "@type": "Service",
      name: content.h1,
      description: content.intro,
      provider: { "@type": "Organization", name: APP_NAME },
      areaServed: ctx.city?.name ?? ctx.state?.name ?? "Brasil",
      url: pageUrl,
    });
  }

  schemas.push({
    "@context": "https://schema.org",
    "@type": "Product",
    name: content.h1,
    description: content.metaDescription,
    brand: { "@type": "Brand", name: APP_NAME },
    offers: {
      "@type": "Offer",
      availability: "https://schema.org/InStock",
      priceCurrency: "BRL",
      url: `${SITE_URL}/contratar`,
    },
  });

  schemas.push(buildBreadcrumbSchema(ctx, content));

  if (content.faq.length > 0) {
    schemas.push(buildFaqSchema(content.faq));
  }

  return schemas;
}

function buildBreadcrumbSchema(
  ctx: SeoPageContext,
  content: SeoPageContent,
): Record<string, unknown> {
  const items: { name: string; item: string }[] = [
    { name: "Início", item: SITE_URL },
  ];

  if (ctx.service) {
    items.push({
      name: ctx.service.name,
      item: `${SITE_URL}/${ctx.service.slug}`,
    });
  }
  if (ctx.segment) {
    items.push({
      name: `App para ${ctx.segment.name}`,
      item: `${SITE_URL}/${buildSegmentSlug(ctx.segment.slug)}`,
    });
  }
  if (ctx.city || ctx.state) {
    items.push({
      name: content.locationLabel,
      item: `${SITE_URL}${content.canonicalPath}`,
    });
  }

  return {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((entry, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: entry.name,
      item: entry.item,
    })),
  };
}

function buildSegmentSlug(segmentSlug: string): string {
  return `app-para-${segmentSlug}`;
}

function buildFaqSchema(faq: SeoFaqItem[]): Record<string, unknown> {
  return {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: faq.map((item) => ({
      "@type": "Question",
      name: item.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: item.answer,
      },
    })),
  };
}

export function buildBlogJsonLd(
  title: string,
  description: string,
  slug: string,
  publishedAt: string,
  faq: SeoFaqItem[],
): Record<string, unknown>[] {
  const url = `${SITE_URL}/blog/${slug}`;
  const schemas: Record<string, unknown>[] = [
    {
      "@context": "https://schema.org",
      "@type": "Article",
      headline: title,
      description,
      datePublished: publishedAt,
      author: { "@type": "Organization", name: APP_NAME },
      publisher: {
        "@type": "Organization",
        name: APP_NAME,
        logo: { "@type": "ImageObject", url: `${SITE_URL}/assets/logo_nogacode_bc.png` },
      },
      mainEntityOfPage: url,
    },
    {
      "@context": "https://schema.org",
      "@type": "BreadcrumbList",
      itemListElement: [
        { "@type": "ListItem", position: 1, name: "Início", item: SITE_URL },
        { "@type": "ListItem", position: 2, name: "Blog", item: `${SITE_URL}/blog` },
        { "@type": "ListItem", position: 3, name: title, item: url },
      ],
    },
  ];
  if (faq.length) schemas.push(buildFaqSchema(faq));
  return schemas;
}
