<?php
/**
 * Review Schema Component
 * Generates Review and AggregateRating structured data
 *
 * Usage:
 * $reviews = [
 *     ['author' => 'John Smith', 'reviewBody' => 'Great service!', 'ratingValue' => 5],
 *     ['author' => 'Jane Doe', 'reviewBody' => 'Excellent work', 'ratingValue' => 5]
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/schema/review-schema.php');
 */

/**
 * Generate Review Schema JSON-LD with AggregateRating
 *
 * @param array $reviews Array of review data
 * @param string $itemName Name of the item being reviewed
 * @param string $itemType Schema type (Organization, Service, Product, etc.)
 * @param string|null $itemUrl URL of the item being reviewed
 * @return string JSON-LD script tag
 */
function generateReviewSchema($reviews, $itemName = 'UK Data Services', $itemType = 'Organization', $itemUrl = null) {
    if (empty($reviews)) {
        return '';
    }

    $baseUrl = 'https://ukdataservices.co.uk';

    // Calculate aggregate rating
    $totalRating = 0;
    $reviewCount = count($reviews);
    foreach ($reviews as $review) {
        $totalRating += $review['ratingValue'] ?? 5;
    }
    $averageRating = round($totalRating / $reviewCount, 1);

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => $itemType,
        'name' => $itemName,
        'url' => $itemUrl ?? $baseUrl,
        'aggregateRating' => [
            '@type' => 'AggregateRating',
            'ratingValue' => number_format($averageRating, 1),
            'reviewCount' => $reviewCount,
            'bestRating' => '5',
            'worstRating' => '1'
        ],
        'review' => array_map(function($review) {
            $reviewSchema = [
                '@type' => 'Review',
                'author' => [
                    '@type' => 'Person',
                    'name' => $review['author']
                ],
                'reviewRating' => [
                    '@type' => 'Rating',
                    'ratingValue' => $review['ratingValue'] ?? 5,
                    'bestRating' => '5',
                    'worstRating' => '1'
                ],
                'reviewBody' => $review['reviewBody']
            ];

            // Add date if provided
            if (isset($review['datePublished'])) {
                $reviewSchema['datePublished'] = $review['datePublished'];
            }

            // Add job title/role if provided
            if (isset($review['authorRole'])) {
                $reviewSchema['author']['jobTitle'] = $review['authorRole'];
            }

            return $reviewSchema;
        }, $reviews)
    ];

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

/**
 * Generate standalone AggregateRating schema (without individual reviews)
 */
function generateAggregateRatingSchema($itemName, $itemType, $ratingValue, $reviewCount, $itemUrl = null) {
    $baseUrl = 'https://ukdataservices.co.uk';

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => $itemType,
        'name' => $itemName,
        'url' => $itemUrl ?? $baseUrl,
        'aggregateRating' => [
            '@type' => 'AggregateRating',
            'ratingValue' => number_format($ratingValue, 1),
            'reviewCount' => $reviewCount,
            'bestRating' => '5',
            'worstRating' => '1'
        ]
    ];

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

/**
 * Predefined testimonials for the homepage and service pages
 */
$siteTestimonials = [
    [
        'author' => 'James Mitchell',
        'authorRole' => 'Retail Director, London Fashion Group',
        'reviewBody' => 'UK Data Services transformed our competitor analysis process. Their web scraping accuracy and speed helped us make better pricing decisions. We\'ve seen a 23% improvement in our market positioning since working with them.',
        'ratingValue' => 5,
        'datePublished' => '2024-09-15'
    ],
    [
        'author' => 'Sarah Chen',
        'authorRole' => 'Head of Strategy, City Fintech Ltd',
        'reviewBody' => 'Outstanding data analytics service. They helped us understand our market position and identify opportunities we\'d completely missed. The team\'s expertise in financial data and GDPR compliance gave us complete confidence.',
        'ratingValue' => 5,
        'datePublished' => '2024-08-22'
    ],
    [
        'author' => 'Michael Thompson',
        'authorRole' => 'Managing Partner, London Property Advisors',
        'reviewBody' => 'Professional, GDPR-compliant, and incredibly responsive. Their property data extraction service has become essential to our London operations. The 99.8% accuracy rate they promised was actually exceeded in our experience.',
        'ratingValue' => 5,
        'datePublished' => '2024-07-10'
    ],
    [
        'author' => 'Emma Williams',
        'authorRole' => 'CEO, Manchester Logistics Solutions',
        'reviewBody' => 'Exceptional service quality. The data cleaning and validation work they did on our supply chain database saved us thousands in operational costs. Highly recommend for any Manchester business.',
        'ratingValue' => 5,
        'datePublished' => '2024-06-28'
    ],
    [
        'author' => 'David Brown',
        'authorRole' => 'Director of Analytics, Birmingham Manufacturing Co',
        'reviewBody' => 'We needed complex automotive parts pricing data across multiple European suppliers. UK Data Services delivered beyond expectations with excellent accuracy and compliance documentation.',
        'ratingValue' => 5,
        'datePublished' => '2024-05-15'
    ],
    [
        'author' => 'Laura Johnson',
        'authorRole' => 'Marketing Director, Scottish Energy Corp',
        'reviewBody' => 'Their competitive intelligence service gave us insights into market movements we couldn\'t have obtained otherwise. Professional team with deep technical expertise.',
        'ratingValue' => 5,
        'datePublished' => '2024-04-20'
    ]
];

/**
 * Service-specific testimonials
 */
$serviceTestimonials = [
    'web-scraping' => [
        [
            'author' => 'Robert Harris',
            'authorRole' => 'CTO, E-commerce Solutions Ltd',
            'reviewBody' => 'Their web scraping capabilities are world-class. They handled complex JavaScript-rendered pages with ease and delivered clean, accurate data exactly as specified.',
            'ratingValue' => 5
        ]
    ],
    'price-monitoring' => [
        [
            'author' => 'Sophie Turner',
            'authorRole' => 'Pricing Manager, UK Retail Chain',
            'reviewBody' => 'The price monitoring alerts have revolutionized how we respond to competitor pricing. We now react in hours instead of days.',
            'ratingValue' => 5
        ]
    ],
    'data-cleaning' => [
        [
            'author' => 'Mark Stevens',
            'authorRole' => 'Data Manager, Healthcare Analytics',
            'reviewBody' => 'They cleaned and standardized over 2 million patient records with 99.9% accuracy. The improvement in our data quality has been transformational.',
            'ratingValue' => 5
        ]
    ]
];

// If $reviews is set, output the schema automatically
if (isset($reviews) && is_array($reviews)) {
    echo generateReviewSchema(
        $reviews,
        $reviewItemName ?? 'UK Data Services',
        $reviewItemType ?? 'Organization',
        $reviewItemUrl ?? null
    );
}
