<script lang="ts">
  import SeoHead from '$lib/components/SeoHead.svelte';
  import Footer from '$lib/components/Footer.svelte';
  import Header from '$lib/components/Header.svelte';
  import HeroSection from '$lib/components/HeroSection.svelte';
  import HowItWorksSection from '$lib/components/HowItWorksSection.svelte';
  import InfoSection from '$lib/components/InfoSection.svelte';
  import InstagramSection from '$lib/components/InstagramSection.svelte';
  import BookingSection from '$lib/components/BookingSection.svelte';
  import PromiseSection from '$lib/components/PromiseSection.svelte';
  import ServicesSection from '$lib/components/ServicesSection.svelte';
  import TestimonialsSection from '$lib/components/TestimonialsSection.svelte';
  import ValuesSection from '$lib/components/ValuesSection.svelte';
  import OnboardingPage from '$lib/components/OnboardingPage.svelte';
  import { buildAreaServed } from '$lib/seo';
  import type { PageData } from './$types';

  export let data: PageData;

  const siteUrl = 'https://www.goodwalk.co.nz';

  function absoluteUrl(value: string) {
    if (value.startsWith('http://') || value.startsWith('https://')) {
      return value;
    }

    return `${siteUrl}${value.startsWith('/') ? value : `/${value}`}`;
  }

  $: homepageStructuredData =
    data.siteVariant === 'marketing' && data.content
      ? [
          {
            '@context': 'https://schema.org',
            '@type': 'WebSite',
            name: 'Goodwalk',
            url: siteUrl,
            inLanguage: 'en-NZ'
          },
          {
            '@context': 'https://schema.org',
            '@type': ['LocalBusiness', 'PetCareService'],
            '@id': 'https://www.goodwalk.co.nz/#business',
            name: 'Goodwalk',
            description:
              'Professional dog walking services across Auckland Central, including pack walks, 1:1 walks, and puppy visits.',
            url: siteUrl,
            logo: `${siteUrl}/images/goodwalk-auckland-dog-walking-logo.png`,
            image: absoluteUrl(data.content.hero.imageUrl),
            email: 'info@goodwalk.co.nz',
            telephone: '+64226421011',
            sameAs: ['https://www.instagram.com/goodwalk.nz/', 'https://g.page/r/CUsvrWPhkYrAEB0/'],
            address: {
              '@type': 'PostalAddress',
              addressLocality: 'Auckland Central',
              addressRegion: 'Auckland',
              addressCountry: 'NZ'
            },
            areaServed: buildAreaServed(),
            openingHoursSpecification: [
              {
                '@type': 'OpeningHoursSpecification',
                dayOfWeek: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
                opens: '08:00',
                closes: '16:00'
              }
            ],
            hasOfferCatalog: {
              '@type': 'OfferCatalog',
              name: 'Dog Walking Services',
              itemListElement: data.content.services.map((service) => ({
                '@type': 'Offer',
                itemOffered: {
                  '@type': 'Service',
                  name: service.title,
                  url: `${siteUrl}${service.href}`
                }
              }))
            },
            aggregateRating: {
              '@type': 'AggregateRating',
              ratingValue: '5.0',
              bestRating: '5',
              worstRating: '1',
              reviewCount: '30'
            },
            review: data.content.testimonials.map((testimonial) => ({
              '@context': 'https://schema.org',
              '@type': 'Review',
              reviewRating: {
                '@type': 'Rating',
                ratingValue: '5',
                bestRating: '5'
              },
              author: {
                '@type': 'Person',
                name: testimonial.reviewer
              },
              reviewBody: testimonial.quote
            }))
          },
          {
            '@context': 'https://schema.org',
            '@type': 'FAQPage',
            mainEntity: data.content.info.faqs.map((faq) => ({
              '@type': 'Question',
              name: faq.question,
              acceptedAnswer: {
                '@type': 'Answer',
                text: faq.answer
              }
            }))
          }
        ]
      : [];
</script>

{#if data.siteVariant === 'onboarding'}
  <OnboardingPage preview={data.isPreview} />
{:else}
  {@const content = data.content!}
  <SeoHead
    title={content.seo.title}
    description={content.seo.description}
    canonicalPath="/"
    image={content.hero.imageUrl}
    imageAlt={content.hero.imageAlt}
    structuredData={homepageStructuredData}
    preloadImage={true}
  />

  <Header navigation={content.navigation} />
  <HeroSection hero={content.hero} reviewCta={content.intro.reviewCta} />
  <PromiseSection promise={content.promise} />
  <ServicesSection services={content.services} />
  <HowItWorksSection content={content.howItWorks} />
  <TestimonialsSection testimonials={content.testimonials} seedKey="/" />
  <ValuesSection values={content.values} />
  <BookingSection booking={content.booking} />
  <InfoSection info={content.info} />
  <InstagramSection instagram={content.instagram} />
  <Footer footer={content.footer} />
{/if}
