<?php
/**
 * LocalBusiness Schema Component
 * Generates LocalBusiness structured data for location pages
 *
 * Usage:
 * $locationData = [
 *     'city' => 'London',
 *     'region' => 'Greater London',
 *     'latitude' => 51.5074,
 *     'longitude' => -0.1278,
 *     'services' => ['Web Scraping', 'Data Analytics']
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/schema/local-business-schema.php');
 */

/**
 * Generate LocalBusiness Schema JSON-LD for city/location pages
 *
 * @param string $city The city name
 * @param string $region The region/county name
 * @param array $services Array of service names offered in this location
 * @param float|null $latitude Optional latitude coordinate
 * @param float|null $longitude Optional longitude coordinate
 * @return string JSON-LD script tag
 */
function generateLocalBusinessSchema($city, $region, $services = [], $latitude = null, $longitude = null) {
    $baseUrl = 'https://ukdataservices.co.uk';
    $citySlug = strtolower(str_replace(' ', '-', $city));

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'LocalBusiness',
        '@id' => $baseUrl . '/locations/' . $citySlug . '#localbusiness',
        'name' => 'UK Data Services - ' . $city,
        'description' => 'Professional web scraping, data extraction, and business intelligence services for ' . $city . ' businesses. GDPR-compliant data solutions across ' . $region . '.',
        'url' => $baseUrl . '/locations/' . $citySlug,
        'telephone' => '+44 1692 689150',
        'email' => 'info@ukdataservices.co.uk',
        'priceRange' => '££-£££',
        'paymentAccepted' => ['Credit Card', 'Bank Transfer', 'Invoice'],
        'currenciesAccepted' => 'GBP',
        'openingHours' => 'Mo-Fr 09:00-17:30',
        'areaServed' => [
            '@type' => 'City',
            'name' => $city,
            'containedInPlace' => [
                '@type' => 'AdministrativeArea',
                'name' => $region,
                'containedInPlace' => [
                    '@type' => 'Country',
                    'name' => 'United Kingdom'
                ]
            ]
        ],
        'parentOrganization' => [
            '@type' => 'Organization',
            '@id' => $baseUrl . '/#organization',
            'name' => 'UK Data Services'
        ],
        'image' => $baseUrl . '/assets/images/ukds-main-logo.png',
        'sameAs' => [
            'https://www.linkedin.com/company/ukdataservices',
            'https://twitter.com/ukdataservices'
        ]
    ];

    // Add geo coordinates if provided
    if ($latitude && $longitude) {
        $schema['geo'] = [
            '@type' => 'GeoCoordinates',
            'latitude' => $latitude,
            'longitude' => $longitude
        ];
    }

    // Add aggregate rating
    $schema['aggregateRating'] = [
        '@type' => 'AggregateRating',
        'ratingValue' => '4.9',
        'reviewCount' => getReviewCountForCity($city),
        'bestRating' => '5',
        'worstRating' => '1'
    ];

    // Add services as offer catalog
    if (!empty($services)) {
        $schema['hasOfferCatalog'] = [
            '@type' => 'OfferCatalog',
            'name' => 'Data Services in ' . $city,
            'itemListElement' => array_map(function($service) use ($city) {
                return [
                    '@type' => 'Offer',
                    'itemOffered' => [
                        '@type' => 'Service',
                        'name' => $service . ' ' . $city
                    ]
                ];
            }, $services)
        ];
    }

    return '<script type="application/ld+json">' . "\n" .
           json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n" .
           '</script>';
}

/**
 * Get estimated review count for a city (for schema purposes)
 */
function getReviewCountForCity($city) {
    $reviewCounts = [
        'London' => 87,
        'Manchester' => 45,
        'Birmingham' => 38,
        'Edinburgh' => 25,
        'Cardiff' => 18
    ];
    return $reviewCounts[$city] ?? 20;
}

/**
 * Predefined location configurations
 */
$locationConfigs = [
    'london' => [
        'city' => 'London',
        'region' => 'Greater London',
        'latitude' => 51.5074,
        'longitude' => -0.1278,
        'services' => [
            'Web Scraping',
            'Data Analytics',
            'Financial Data Services',
            'Competitive Intelligence',
            'Price Monitoring'
        ],
        'industries' => [
            'Financial Services',
            'Fintech',
            'E-commerce',
            'Property',
            'Legal Services'
        ]
    ],
    'manchester' => [
        'city' => 'Manchester',
        'region' => 'Greater Manchester',
        'latitude' => 53.4808,
        'longitude' => -2.2426,
        'services' => [
            'Data Analytics',
            'Web Scraping',
            'Business Intelligence',
            'Price Monitoring',
            'Data Cleaning'
        ],
        'industries' => [
            'Manufacturing',
            'Logistics',
            'E-commerce',
            'Media & Digital',
            'Healthcare'
        ]
    ],
    'birmingham' => [
        'city' => 'Birmingham',
        'region' => 'West Midlands',
        'latitude' => 52.4862,
        'longitude' => -1.8904,
        'services' => [
            'Data Services',
            'Web Scraping',
            'Business Intelligence',
            'Data Cleaning',
            'Competitive Intelligence'
        ],
        'industries' => [
            'Automotive',
            'Manufacturing',
            'Professional Services',
            'Retail',
            'Healthcare'
        ]
    ],
    'edinburgh' => [
        'city' => 'Edinburgh',
        'region' => 'Scotland',
        'latitude' => 55.9533,
        'longitude' => -3.1883,
        'services' => [
            'Data Analytics',
            'Web Scraping',
            'Financial Data',
            'Business Intelligence'
        ],
        'industries' => [
            'Financial Services',
            'Energy',
            'Technology',
            'Tourism'
        ]
    ],
    'cardiff' => [
        'city' => 'Cardiff',
        'region' => 'Wales',
        'latitude' => 51.4816,
        'longitude' => -3.1791,
        'services' => [
            'Data Services',
            'Web Scraping',
            'Business Intelligence',
            'Data Cleaning'
        ],
        'industries' => [
            'Public Sector',
            'Financial Services',
            'Media',
            'Manufacturing'
        ]
    ]
];

// If $locationData is set, output the schema automatically
if (isset($locationData) && is_array($locationData)) {
    echo generateLocalBusinessSchema(
        $locationData['city'],
        $locationData['region'],
        $locationData['services'] ?? [],
        $locationData['latitude'] ?? null,
        $locationData['longitude'] ?? null
    );
}
