// seo.jsx — per-route metadata, Open Graph, and JSON-LD structured data
const SITE_URL = 'https://escondidochildcaredaycare.com/';
const OG_IMAGE = SITE_URL + 'uploads/hero-main.jpg';
const OG_LOCALE = { en: 'en_US', zh: 'zh_CN', es: 'es_ES' };
const HTML_LANG = { en: 'en', zh: 'zh-CN', es: 'es' };
const SEO_KEYWORDS = {
  en: 'daycare Escondido, childcare Escondido CA, family daycare 92026, in-home daycare Escondido, licensed home daycare, infant daycare Escondido, toddler care Escondido, preschool Escondido, pre-kindergarten North County San Diego, daycare near me Escondido, bilingual daycare, kids martial arts Escondido',
  zh: '埃斯孔迪多托儿所, 埃斯孔迪多家庭托育, 圣地亚哥中文托育, 加州持照托育园, 92026 托儿所, 婴儿托育, 幼儿托育, 学前班, 北郡托儿所, 儿童武术课, 双语托育',
  es: 'guardería Escondido, cuidado infantil Escondido CA, guardería familiar 92026, guardería en casa con licencia, cuidado de bebés Escondido, preescolar Escondido, guardería cerca de mí, cuidado infantil bilingüe, artes marciales para niños',
};

function metaTag(attr, key, content) {
  let el = document.head.querySelector(`meta[${attr}="${key}"]`);
  if (!el) { el = document.createElement('meta'); el.setAttribute(attr, key); document.head.appendChild(el); }
  el.setAttribute('content', content);
}
function linkTag(rel, href, extra) {
  const sel = extra && extra.hreflang ? `link[rel="${rel}"][hreflang="${extra.hreflang}"]` : `link[rel="${rel}"]`;
  let el = document.head.querySelector(sel);
  if (!el) { el = document.createElement('link'); el.setAttribute('rel', rel); if (extra && extra.hreflang) el.setAttribute('hreflang', extra.hreflang); document.head.appendChild(el); }
  el.setAttribute('href', href);
}
function jsonLd(id, data) {
  let el = document.getElementById(id);
  if (!el) { el = document.createElement('script'); el.type = 'application/ld+json'; el.id = id; document.head.appendChild(el); }
  el.textContent = JSON.stringify(data);
}

const BUSINESS = {
  '@type': ['ChildCare', 'LocalBusiness'],
  '@id': SITE_URL + '#business',
  name: 'Seven Little Bears Family Childcare',
  alternateName: ['Seven Little Bears Family Daycare', '七只小熊家庭托育园'],
  url: SITE_URL,
  image: [OG_IMAGE, SITE_URL + 'uploads/indoor-room.jpg'],
  logo: OG_IMAGE,
  telephone: '+1-858-397-4659',
  priceRange: '$$',
  currenciesAccepted: 'USD',
  address: { '@type': 'PostalAddress', streetAddress: '1952 N Ash St', addressLocality: 'Escondido', addressRegion: 'CA', postalCode: '92026', addressCountry: 'US' },
  hasMap: 'https://maps.app.goo.gl/2vstGRhWcyBkvQqZ6',
  keywords: 'daycare Escondido, family childcare Escondido CA, licensed in-home daycare 92026, infant and toddler care, preschool, pre-kindergarten, bilingual childcare, kids martial arts',
  serviceArea: { '@type': 'GeoCircle', geoMidpoint: { '@type': 'GeoCoordinates', address: { '@type': 'PostalAddress', postalCode: '92026', addressLocality: 'Escondido', addressRegion: 'CA', addressCountry: 'US' } }, geoRadius: '16000' },
  paymentAccepted: 'Cash, Check, Zelle',
  isAccessibleForFree: false,
  areaServed: [
    { '@type': 'City', name: 'Escondido' }, { '@type': 'City', name: 'San Marcos' },
    { '@type': 'City', name: 'Valley Center' }, { '@type': 'City', name: 'Vista' },
    { '@type': 'Place', name: 'Hidden Meadows' }, { '@type': 'PostalCodeRangeSpecification', postalCodeBegin: '92026', postalCodeEnd: '92029' },
    { '@type': 'AdministrativeArea', name: 'North County San Diego' },
  ],
  openingHoursSpecification: [{ '@type': 'OpeningHoursSpecification', dayOfWeek: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], opens: '07:00', closes: '17:30' }],
  foundingDate: '2021',
  knowsLanguage: ['en', 'zh', 'es'],
  slogan: 'Every child is a little seed.',
  identifier: { '@type': 'PropertyValue', name: 'California family childcare license', value: '376300341' },
  audience: { '@type': 'PeopleAudience', suggestedMinAge: 0.25, suggestedMaxAge: 5 },
  hasOfferCatalog: {
    '@type': 'OfferCatalog', name: 'Childcare programs',
    itemListElement: [
      { '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Toddler program (3 months – 2 years)', serviceType: 'Infant and toddler childcare' } },
      { '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Two-year-old program', serviceType: 'Preschool childcare' } },
      { '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Three-year-old and up program (3 – 5 years)', serviceType: 'Pre-kindergarten childcare' } },
      { '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Sports, music and writing classes', serviceType: 'Enrichment classes' } },
    ],
  },
};

function applySEO(path, lang, meta) {
  const i = { en: 0, zh: 1, es: 2 }[lang] ?? 0;
  const title = meta[0][i];
  const desc = meta[1][i];
  const file = window.purl ? window.purl(path) : 'index.html';
  const url = SITE_URL + lang + '/' + (file === 'index.html' ? '' : file);

  document.title = title;
  metaTag('name', 'description', desc);
  metaTag('name', 'keywords', SEO_KEYWORDS[lang] || SEO_KEYWORDS.en);
  metaTag('name', 'robots', 'index, follow, max-image-preview:large, max-snippet:-1');
  metaTag('name', 'author', 'Seven Little Bears Family Childcare');
  metaTag('name', 'geo.region', 'US-CA');
  metaTag('name', 'geo.placename', 'Escondido, California');
  metaTag('property', 'og:type', 'website');
  metaTag('property', 'og:site_name', 'Seven Little Bears Family Childcare');
  metaTag('property', 'og:title', title);
  metaTag('property', 'og:description', desc);
  metaTag('property', 'og:url', url);
  metaTag('property', 'og:image', OG_IMAGE);
  metaTag('property', 'og:image:alt', 'Seven Little Bears Family Childcare');
  metaTag('property', 'og:locale', OG_LOCALE[lang] || 'en_US');
  metaTag('name', 'twitter:card', 'summary_large_image');
  metaTag('name', 'twitter:title', title);
  metaTag('name', 'twitter:description', desc);
  metaTag('name', 'twitter:image', OG_IMAGE);
  linkTag('canonical', url);

  const crumbs = [{ '@type': 'ListItem', position: 1, name: 'Home', item: SITE_URL }];
  if (path !== '/') crumbs.push({ '@type': 'ListItem', position: 2, name: title.split(' · ')[0], item: url });

  const graph = [
    BUSINESS,
    { '@type': 'WebSite', '@id': SITE_URL + '#website', url: SITE_URL, name: 'Seven Little Bears Family Childcare', publisher: { '@id': SITE_URL + '#business' }, inLanguage: ['en', 'zh-CN', 'es'] },
    { '@type': 'WebPage', '@id': url, url, name: title, description: desc, inLanguage: HTML_LANG[lang], isPartOf: { '@id': SITE_URL + '#website' }, about: { '@id': SITE_URL + '#business' }, primaryImageOfPage: OG_IMAGE },
    { '@type': 'BreadcrumbList', itemListElement: crumbs },
  ];

  if (path === '/faqs' && window.FAQS) {
    graph.push({
      '@type': 'FAQPage',
      mainEntity: window.FAQS.map(([q, a]) => ({
        '@type': 'Question', name: q[i === 1 ? 1 : 0],
        acceptedAnswer: { '@type': 'Answer', text: String(a[i === 1 ? 1 : 0]).replace(/\s+/g, ' ').trim() },
      })),
    });
  }
  if (path === '/tour') {
    graph.push({ '@type': 'ContactPage', '@id': url + '#contact', url, name: title, about: { '@id': SITE_URL + '#business' } });
  }

  jsonLd('ld-graph', { '@context': 'https://schema.org', '@graph': graph });
}

Object.assign(window, { applySEO, SITE_URL });
