import { site } from '../config/site';
import { faqs } from '../data/content';
import type { Plan } from '../data/plans';
import { LANGS, localize, type Lang } from '../i18n';
import { planText } from './plans';

/** FAQPage structured data */
export function faqLd(list: readonly { q: string; a: string }[] = faqs) {
  return {
    '@context': 'https://schema.org',
    '@type': 'FAQPage',
    mainEntity: list.map((f) => ({
      '@type': 'Question',
      name: f.q,
      acceptedAnswer: { '@type': 'Answer', text: f.a },
    })),
  };
}

/** SoftwareApplication with one Offer per standard plan (monthly price) */
export function softwareLd(plans?: Plan[], lang: Lang = 'en') {
  const base: Record<string, unknown> = {
    '@context': 'https://schema.org',
    '@type': 'SoftwareApplication',
    name: site.name,
    applicationCategory: 'BusinessApplication',
    applicationSubCategory: 'Fleet management / Transportation management',
    operatingSystem: 'Web, Android, iOS (installable PWA)',
    description: lang === 'en' ? site.description
      : 'Software de transporte para flotas pequeñas: hojas de viaje digitales en el teléfono, millas IFTA por estado, gastos con recibos y pago semanal de choferes.',
    inLanguage: LANGS[lang].hreflang,
    url: `${site.url}${localize('/', lang)}`.replace(/\/$/, '') || site.url,
    image: `${site.url}${site.ogImage}`,
  };
  if (plans?.length) {
    base.offers = plans.map((p) => ({
      '@type': 'Offer',
      name: p.name,
      description: planText(p, lang).description || undefined,
      price: p.price_monthly.toFixed(2),
      priceCurrency: site.currency,
      category: 'subscription',
      url: `${site.url}${localize('/pricing/', lang)}`,
      priceSpecification: {
        '@type': 'UnitPriceSpecification',
        price: p.price_monthly.toFixed(2),
        priceCurrency: site.currency,
        unitCode: 'MON',
        referenceQuantity: { '@type': 'QuantitativeValue', value: 1, unitCode: 'MON' },
      },
    }));
  }
  return base;
}

export function breadcrumbLd(items: { name: string; path: string }[]) {
  return {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement: items.map((it, i) => ({
      '@type': 'ListItem',
      position: i + 1,
      name: it.name,
      item: `${site.url}${it.path}`,
    })),
  };
}
