import { locationPages } from '$lib/content/locations';
import type { LocationPageContent } from '$lib/types';

const siteUrl = 'https://www.goodwalk.co.nz';
const defaultLocationImage = '/images/auckland-pack-walk-small-dogs-group.jpg';
const defaultLocationImageAlt = 'Goodwalk Auckland dog walking services';

interface BreadcrumbItem {
  name: string;
  path?: string;
  url?: string;
}

export function absoluteUrl(value: string) {
  if (!value) {
    return siteUrl;
  }

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

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

export function buildBreadcrumb(items: BreadcrumbItem[]) {
  return {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement: items.map((item, index) => ({
      '@type': 'ListItem',
      position: index + 1,
      name: item.name,
      item: item.url ?? absoluteUrl(item.path ?? '/')
    }))
  };
}

export function buildAreaServed(locations = locationPages) {
  return locations.map((location) => ({
    '@type': 'Place',
    name: `${location.suburb}, Auckland, New Zealand`
  }));
}

function getLocationSeoImage(location: LocationPageContent) {
  return location.seo?.image ?? location.parks.find((park) => park.image?.src)?.image?.src ?? defaultLocationImage;
}

function getLocationSeoImageAlt(location: LocationPageContent) {
  return (
    location.seo?.imageAlt ??
    location.parks.find((park) => park.image?.alt)?.image?.alt ??
    `Goodwalk dog walkers in ${location.suburb}, Auckland`
  );
}

export function buildLocationSeo(location: LocationPageContent) {
  const canonicalPath = `/locations/${location.slug}`;
  const title =
    location.seo?.title ?? `Dog Walkers in ${location.suburb} | Goodwalk Auckland`;
  const description =
    location.seo?.description ??
    `Goodwalk provides pack walks, 1:1 walks, and puppy visits in ${location.suburb}, Auckland Central. Small dog specialists with free pickup and drop-off. Book a free Meet & Greet.`;
  const image = getLocationSeoImage(location);
  const imageAlt = getLocationSeoImageAlt(location);
  const breadcrumbLabel = location.seo?.breadcrumbLabel ?? location.suburb;
  const serviceName = location.seo?.serviceName ?? `Goodwalk Dog Walking - ${location.suburb}`;
  const serviceType = location.seo?.serviceType ?? 'Dog walking and puppy visits';

  return {
    title,
    description,
    canonicalPath,
    image,
    imageAlt,
    structuredData: [
      {
        '@context': 'https://schema.org',
        '@type': 'Service',
        name: serviceName,
        description,
        serviceType,
        provider: {
          '@id': `${siteUrl}/#business`
        },
        areaServed: {
          '@type': 'Place',
          name: `${location.suburb}, Auckland, New Zealand`
        },
        image: absoluteUrl(image),
        url: absoluteUrl(canonicalPath)
      },
      buildBreadcrumb([
        { name: 'Home', url: siteUrl },
        { name: breadcrumbLabel, path: canonicalPath }
      ])
    ] as Record<string, unknown>[]
  };
}
