<?php
/**
 * Service Schema Component
 * Generates Service structured data for improved SEO
 *
 * Usage:
 * $serviceData = [
 *     'name' => 'Web Scraping Services',
 *     'description' => 'Professional web scraping services...',
 *     'url' => 'https://ukdataservices.co.uk/services/web-scraping',
 *     'serviceType' => 'Web Scraping',
 *     'priceRange' => '500-50000',
 *     'features' => ['Feature 1', 'Feature 2']
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/schema/service-schema.php');
 */

/**
 * Generate Service Schema JSON-LD
 *
 * @param string $serviceName The name of the service
 * @param string $serviceDescription A detailed description of the service
 * @param string $serviceUrl The canonical URL of the service page
 * @param string $serviceType The type/category of service
 * @param string|null $priceRange Optional price range (e.g., "500-50000")
 * @param array $features Optional array of service features
 * @return string JSON-LD script tag
 */
function generateServiceSchema($serviceName, $serviceDescription, $serviceUrl, $serviceType, $priceRange = null, $features = []) {
    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'Service',
        '@id' => $serviceUrl . '#service',
        'name' => $serviceName,
        'description' => $serviceDescription,
        'url' => $serviceUrl,
        'serviceType' => $serviceType,
        'provider' => [
            '@type' => 'Organization',
            '@id' => 'https://ukdataservices.co.uk/#organization',
            'name' => 'UK Data Services',
            'url' => 'https://ukdataservices.co.uk'
        ],
        'areaServed' => [
            '@type' => 'Country',
            'name' => 'United Kingdom'
        ],
        'availableChannel' => [
            '@type' => 'ServiceChannel',
            'serviceUrl' => 'https://ukdataservices.co.uk/quote',
            'servicePhone' => '+44 1692 689150',
            'availableLanguage' => 'English'
        ]
    ];

    // Add price specification if provided
    if ($priceRange) {
        $prices = explode('-', $priceRange);
        $schema['offers'] = [
            '@type' => 'Offer',
            'priceCurrency' => 'GBP',
            'priceSpecification' => [
                '@type' => 'PriceSpecification',
                'minPrice' => (float)$prices[0],
                'maxPrice' => isset($prices[1]) ? (float)$prices[1] : null,
                'priceCurrency' => 'GBP'
            ],
            'availability' => 'https://schema.org/InStock',
            'validFrom' => date('Y-m-d')
        ];
    }

    // Add features/service output if provided
    if (!empty($features)) {
        $schema['hasOfferCatalog'] = [
            '@type' => 'OfferCatalog',
            'name' => $serviceName . ' Features',
            'itemListElement' => array_map(function($feature) {
                return [
                    '@type' => 'Offer',
                    'itemOffered' => [
                        '@type' => 'Service',
                        'name' => $feature
                    ]
                ];
            }, $features)
        ];
    }

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

/**
 * Predefined service configurations for consistency
 */
$serviceConfigs = [
    'web-scraping' => [
        'name' => 'Web Scraping Services UK',
        'description' => 'Professional web scraping and data extraction services for UK businesses. Automated data collection from websites with 99.8% accuracy and full GDPR compliance.',
        'url' => 'https://ukdataservices.co.uk/services/web-scraping',
        'serviceType' => 'Web Scraping',
        'priceRange' => '500-50000',
        'features' => [
            'Automated data extraction',
            'Real-time monitoring',
            'GDPR-compliant collection',
            'Custom API delivery',
            'Multi-format output'
        ]
    ],
    'competitive-intelligence' => [
        'name' => 'Competitive Intelligence Services UK',
        'description' => 'Strategic competitive intelligence and market analysis services. Monitor competitors, track market trends, and gain actionable business insights.',
        'url' => 'https://ukdataservices.co.uk/services/competitive-intelligence',
        'serviceType' => 'Competitive Intelligence',
        'priceRange' => '1000-25000',
        'features' => [
            'Competitor monitoring',
            'Market trend analysis',
            'Price tracking',
            'Product intelligence',
            'Strategic reporting'
        ]
    ],
    'price-monitoring' => [
        'name' => 'Price Monitoring Services UK',
        'description' => 'Real-time price monitoring and competitor price tracking services. E-commerce pricing intelligence for UK retailers and brands.',
        'url' => 'https://ukdataservices.co.uk/services/price-monitoring',
        'serviceType' => 'Price Monitoring',
        'priceRange' => '500-15000',
        'features' => [
            'Real-time price tracking',
            'Competitor price alerts',
            'MAP monitoring',
            'Historical price analysis',
            'Automated reporting'
        ]
    ],
    'data-cleaning' => [
        'name' => 'Data Cleaning & Validation Services UK',
        'description' => 'Professional data cleaning, validation, and standardisation services. Transform messy data into accurate, structured datasets.',
        'url' => 'https://ukdataservices.co.uk/services/data-cleaning',
        'serviceType' => 'Data Cleaning',
        'priceRange' => '500-20000',
        'features' => [
            'Duplicate removal',
            'Data standardisation',
            'Format validation',
            'Error correction',
            'Quality assurance'
        ]
    ],
    'data-analytics' => [
        'name' => 'Data Analytics Services UK',
        'description' => 'Business intelligence and data analytics solutions for UK enterprises. Transform raw data into actionable insights.',
        'url' => 'https://ukdataservices.co.uk/services/data-analytics',
        'serviceType' => 'Data Analytics',
        'priceRange' => '1000-30000',
        'features' => [
            'Business intelligence',
            'Predictive analytics',
            'Custom dashboards',
            'Data visualisation',
            'Strategic insights'
        ]
    ],
    'api-development' => [
        'name' => 'API Development Services UK',
        'description' => 'Custom API development and data integration services. Build robust APIs to connect your systems and automate data workflows.',
        'url' => 'https://ukdataservices.co.uk/services/api-development',
        'serviceType' => 'API Development',
        'priceRange' => '2000-40000',
        'features' => [
            'Custom API design',
            'System integration',
            'Real-time data feeds',
            'Authentication systems',
            'Documentation'
        ]
    ],
    'property-data-extraction' => [
        'name' => 'UK Property Data Extraction Services',
        'description' => 'Professional property data extraction from UK property portals including Rightmove, Zoopla, and OnTheMarket. GDPR-compliant property market intelligence.',
        'url' => 'https://ukdataservices.co.uk/services/property-data-extraction',
        'serviceType' => 'Property Data Extraction',
        'priceRange' => '1000-25000',
        'features' => [
            'Property portal data extraction',
            'Market analysis',
            'Investment research data',
            'Rental market intelligence',
            'Commercial property data'
        ]
    ],
    'financial-data-services' => [
        'name' => 'Financial Data Services UK',
        'description' => 'FCA-aware financial data services for hedge funds, asset managers, and investment firms. Market data extraction and alternative data solutions.',
        'url' => 'https://ukdataservices.co.uk/services/financial-data-services',
        'serviceType' => 'Financial Data Services',
        'priceRange' => '5000-100000',
        'features' => [
            'Market data extraction',
            'Alternative data feeds',
            'Securities monitoring',
            'Compliance-aware collection',
            'API delivery'
        ]
    ]
];

// If $serviceData is set, output the schema automatically
if (isset($serviceData) && is_array($serviceData)) {
    echo generateServiceSchema(
        $serviceData['name'],
        $serviceData['description'],
        $serviceData['url'],
        $serviceData['serviceType'],
        $serviceData['priceRange'] ?? null,
        $serviceData['features'] ?? []
    );
}
