<?php
/**
 * Breadcrumb Schema Component
 * Generates BreadcrumbList structured data for improved SEO
 *
 * Usage: Set $breadcrumbs array before including this file
 * Example:
 * $breadcrumbs = [
 *     ['url' => '/', 'label' => 'Home'],
 *     ['url' => '/blog', 'label' => 'Blog'],
 *     ['url' => '', 'label' => 'Current Page Title']
 * ];
 */

if (!isset($breadcrumbs) || empty($breadcrumbs)) {
    return;
}

$base_url = 'https://ukdataservices.co.uk';
$items = [];

foreach ($breadcrumbs as $index => $crumb) {
    $position = $index + 1;
    $item = [
        '@type' => 'ListItem',
        'position' => $position,
        'name' => $crumb['label']
    ];

    // Add URL for all items except the last one (current page)
    if (!empty($crumb['url'])) {
        $url = $crumb['url'];
        // Ensure URL is absolute
        if (strpos($url, 'http') !== 0) {
            $url = $base_url . $url;
        }
        $item['item'] = $url;
    }

    $items[] = $item;
}

$schema = [
    '@context' => 'https://schema.org',
    '@type' => 'BreadcrumbList',
    'itemListElement' => $items
];
?>

<script type="application/ld+json">
<?php echo json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); ?>
</script>
