// Shared SEO + meta tag helper. Injects per-page <title>, meta description,
// canonical, OG tags, Twitter card, and JSON-LD on mount. Call once per page.
//
// Usage: <window.WeSEO page="home" />  or  <window.WeSEO page="services" />
//
// SITE-WIDE SEO POSTURE:
// - Toronto / GTA local SEO: business address + service area in JSON-LD,
//   "Toronto" and "GTA" worked into titles/descriptions where natural.
// - AI crawlers welcomed: see /llms.txt and /robots.txt.

const SITE = {
  url: 'https://leviyet.com',
  name: 'Leviyet',
  tagline: 'AI consulting for Toronto businesses that need to move',
  description: "Toronto-based AI consultancy helping mid-market companies across the GTA cut busywork, ship faster, and grow revenue — with AI when it fits, and the simpler tool when it doesn't.",
  city: 'Toronto',
  region: 'ON',
  country: 'CA',
  phone: '+1-416-555-0142',
  email: 'hello@leviyet.com',
};

const PAGES = {
  home: {
    title: 'Leviyet — AI Consulting & Custom Agents · Toronto',
    desc: "Toronto AI consultancy. We audit, build, and train — agents, automations, AI marketing — for mid-market companies across the GTA. AI when it fits; the simpler tool when it doesn't.",
    path: '/',
  },
  services: {
    title: 'Services — AI Audits, Agents, Marketing & Web · Leviyet Toronto',
    desc: 'Six ways we partner with Toronto and GTA businesses: AI opportunity audit, custom agents, AI marketing, web design, ads & SEO, and team enablement.',
    path: '/services',
  },
  process: {
    title: 'How We Work — Listen, Build, Hand Over · Leviyet',
    desc: "Our process: shadow your team, find what's slow, build the smallest thing that earns its keep, then teach you to run it. No big-bang launches.",
    path: '/process',
  },
  work: {
    title: 'Selected Work — AI Case Studies · Leviyet Toronto',
    desc: '4 partnerships: a SaaS support agent, a law firm research assistant, a clinic booking system, and a manufacturer knowledge base. Real outcomes, named in numbers.',
    path: '/work',
  },
  contact: {
    title: 'Contact — Book a 30-min Intro Call · Leviyet Toronto',
    desc: "Tell us what you're working on. 30-minute intro call, no deck, no pitch. Toronto-based, serving the GTA and beyond. Reply within one business day.",
    path: '/contact',
  },
  faq: {
    title: 'FAQ — Common Questions About AI Consulting · Leviyet',
    desc: 'How long until we see results? Will our data be used to train models? When is AI not the answer? Honest answers to the questions clients actually ask.',
    path: '/faq',
  },
  privacy: {
    title: 'Privacy Policy · Leviyet',
    desc: 'How Leviyet handles your information. Plain language, real people, no dark patterns.',
    path: '/privacy',
  },
  terms: {
    title: 'Terms of Service · Leviyet',
    desc: 'Terms governing use of leviyet.com and free resources. Paid engagements run under a separate signed MSA.',
    path: '/terms',
  },
  thanks: {
    title: 'Got it — We\'ll be in touch · Leviyet',
    desc: 'Thanks for reaching out. We respond within one business day.',
    path: '/thanks',
    noindex: true,
  },
  notfound: {
    title: '404 — Page not found · Leviyet',
    desc: 'The page you were looking for has moved or never existed.',
    path: '/404',
    noindex: true,
  },
};

const WeSEO = ({ page = 'home' }) => {
  React.useEffect(() => {
    const meta = PAGES[page] || PAGES.home;
    const url = SITE.url + meta.path;

    document.title = meta.title;

    const set = (selector, attrs, content) => {
      let el = document.head.querySelector(selector);
      if (!el) {
        el = document.createElement(attrs.tag || 'meta');
        Object.entries(attrs).forEach(([k, v]) => k !== 'tag' && el.setAttribute(k, v));
        document.head.appendChild(el);
      }
      if (content !== undefined) {
        if (el.tagName === 'META') el.setAttribute('content', content);
        else el.textContent = content;
      }
    };

    set('meta[name="description"]', { name: 'description' }, meta.desc);
    set('link[rel="canonical"]', { tag: 'link', rel: 'canonical', href: url });
    set('meta[name="robots"]', { name: 'robots' }, meta.noindex ? 'noindex, nofollow' : 'index, follow, max-image-preview:large');
    set('meta[name="author"]', { name: 'author' }, SITE.name);
    set('meta[name="geo.region"]', { name: 'geo.region' }, 'CA-ON');
    set('meta[name="geo.placename"]', { name: 'geo.placename' }, 'Toronto');

    // Open Graph
    set('meta[property="og:type"]', { property: 'og:type' }, page === 'home' ? 'website' : 'article');
    set('meta[property="og:title"]', { property: 'og:title' }, meta.title);
    set('meta[property="og:description"]', { property: 'og:description' }, meta.desc);
    set('meta[property="og:url"]', { property: 'og:url' }, url);
    set('meta[property="og:site_name"]', { property: 'og:site_name' }, SITE.name);
    set('meta[property="og:locale"]', { property: 'og:locale' }, 'en_CA');
    set('meta[property="og:image"]', { property: 'og:image' }, SITE.url + '/og-image.png');
    set('meta[property="og:image:width"]', { property: 'og:image:width' }, '1200');
    set('meta[property="og:image:height"]', { property: 'og:image:height' }, '630');

    // Twitter
    set('meta[name="twitter:card"]', { name: 'twitter:card' }, 'summary_large_image');
    set('meta[name="twitter:site"]', { name: 'twitter:site' }, '@leviyet');
    set('meta[name="twitter:title"]', { name: 'twitter:title' }, meta.title);
    set('meta[name="twitter:description"]', { name: 'twitter:description' }, meta.desc);
    set('meta[name="twitter:image"]', { name: 'twitter:image' }, SITE.url + '/og-image.png');

    // JSON-LD: organization + local business + sameAs (one per page)
    let ld = document.getElementById('we-jsonld');
    if (!ld) {
      ld = document.createElement('script');
      ld.id = 'we-jsonld';
      ld.type = 'application/ld+json';
      document.head.appendChild(ld);
    }
    ld.textContent = JSON.stringify({
      '@context': 'https://schema.org',
      '@graph': [
        {
          '@type': ['Organization', 'ProfessionalService', 'LocalBusiness'],
          '@id': SITE.url + '#org',
          name: SITE.name,
          url: SITE.url,
          email: SITE.email,
          telephone: SITE.phone,
          description: SITE.description,
          address: {
            '@type': 'PostalAddress',
            addressLocality: 'Toronto',
            addressRegion: 'ON',
            addressCountry: 'CA',
          },
          areaServed: [
            { '@type': 'City', name: 'Toronto' },
            { '@type': 'AdministrativeArea', name: 'Greater Toronto Area' },
            { '@type': 'AdministrativeArea', name: 'Ontario' },
            { '@type': 'Country', name: 'Canada' },
          ],
          serviceType: [
            'AI Consulting', 'AI Opportunity Audit', 'Custom AI Agents',
            'AI Marketing', 'Web Design', 'SEO', 'Paid Advertising', 'AI Training',
          ],
          sameAs: [
            'https://www.linkedin.com/company/leviyet/',
            'https://www.instagram.com/leviyet.digital/',
            'https://www.facebook.com/Leviyet/',
            'https://twitter.com/leviyet',
          ],
          priceRange: '$$$',
          openingHoursSpecification: {
            '@type': 'OpeningHoursSpecification',
            dayOfWeek: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
            opens: '09:00', closes: '17:00',
          },
        },
        {
          '@type': 'WebSite',
          '@id': SITE.url + '#site',
          url: SITE.url,
          name: SITE.name,
          publisher: { '@id': SITE.url + '#org' },
          inLanguage: 'en-CA',
        },
        {
          '@type': 'WebPage',
          '@id': url + '#page',
          url,
          name: meta.title,
          description: meta.desc,
          isPartOf: { '@id': SITE.url + '#site' },
          about: { '@id': SITE.url + '#org' },
        },
      ],
    });
  }, [page]);

  return null;
};

window.WeSEO = WeSEO;
window.WE_SITE = SITE;
