<?php
/**
 * Article Schema Component
 * Generates Article structured data for blog posts
 *
 * Usage:
 * $articleData = [
 *     'title' => 'Article Title',
 *     'description' => 'Article description...',
 *     'datePublished' => '2024-01-15',
 *     'dateModified' => '2024-01-20',
 *     'authorName' => 'John Smith',
 *     'imageUrl' => 'https://example.com/image.jpg',
 *     'articleUrl' => 'https://example.com/article'
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/schema/article-schema.php');
 */

/**
 * Generate Article Schema JSON-LD
 *
 * @param string $title Article title
 * @param string $description Article description/excerpt
 * @param string $datePublished Publication date (Y-m-d format)
 * @param string $dateModified Last modified date (Y-m-d format)
 * @param string $authorName Author's name
 * @param string $imageUrl Featured image URL
 * @param string $articleUrl Canonical article URL
 * @param string $category Optional article category
 * @param array $keywords Optional array of keywords
 * @return string JSON-LD script tag
 */
function generateArticleSchema($title, $description, $datePublished, $dateModified, $authorName, $imageUrl, $articleUrl, $category = null, $keywords = []) {
    $baseUrl = 'https://ukdataservices.co.uk';

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'Article',
        '@id' => $articleUrl . '#article',
        'headline' => $title,
        'description' => $description,
        'url' => $articleUrl,
        'datePublished' => $datePublished . 'T09:00:00+00:00',
        'dateModified' => $dateModified . 'T09:00:00+00:00',
        'author' => [
            '@type' => 'Person',
            'name' => $authorName,
            'url' => $baseUrl . '/about'
        ],
        'publisher' => [
            '@type' => 'Organization',
            '@id' => $baseUrl . '/#organization',
            'name' => 'UK Data Services',
            'logo' => [
                '@type' => 'ImageObject',
                'url' => $baseUrl . '/assets/images/ukds-main-logo.png'
            ]
        ],
        'image' => [
            '@type' => 'ImageObject',
            'url' => $imageUrl,
            'width' => 1200,
            'height' => 630
        ],
        'mainEntityOfPage' => [
            '@type' => 'WebPage',
            '@id' => $articleUrl
        ],
        'isPartOf' => [
            '@type' => 'Blog',
            '@id' => $baseUrl . '/blog/#blog',
            'name' => 'UK Data Services Blog',
            'publisher' => [
                '@id' => $baseUrl . '/#organization'
            ]
        ],
        'inLanguage' => 'en-GB'
    ];

    // Add category if provided
    if ($category) {
        $schema['articleSection'] = $category;
    }

    // Add keywords if provided
    if (!empty($keywords)) {
        $schema['keywords'] = implode(', ', $keywords);
    }

    // Add word count estimate based on description length (rough estimate)
    $schema['wordCount'] = max(500, strlen($description) * 5);

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

/**
 * Generate BlogPosting Schema (more specific than Article)
 */
function generateBlogPostingSchema($title, $description, $datePublished, $dateModified, $authorName, $imageUrl, $articleUrl, $category = null, $keywords = []) {
    $baseUrl = 'https://ukdataservices.co.uk';

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'BlogPosting',
        '@id' => $articleUrl . '#blogposting',
        'headline' => $title,
        'description' => $description,
        'url' => $articleUrl,
        'datePublished' => $datePublished . 'T09:00:00+00:00',
        'dateModified' => $dateModified . 'T09:00:00+00:00',
        'author' => [
            '@type' => 'Person',
            'name' => $authorName,
            'url' => $baseUrl . '/about',
            'worksFor' => [
                '@type' => 'Organization',
                '@id' => $baseUrl . '/#organization'
            ]
        ],
        'publisher' => [
            '@type' => 'Organization',
            '@id' => $baseUrl . '/#organization',
            'name' => 'UK Data Services',
            'logo' => [
                '@type' => 'ImageObject',
                'url' => $baseUrl . '/assets/images/ukds-main-logo.png',
                'width' => 300,
                'height' => 100
            ]
        ],
        'image' => [
            '@type' => 'ImageObject',
            'url' => $imageUrl,
            'width' => 1200,
            'height' => 630
        ],
        'mainEntityOfPage' => [
            '@type' => 'WebPage',
            '@id' => $articleUrl
        ],
        'isPartOf' => [
            '@type' => 'Blog',
            '@id' => $baseUrl . '/blog/#blog',
            'name' => 'UK Data Services Blog'
        ],
        'inLanguage' => 'en-GB'
    ];

    if ($category) {
        $schema['articleSection'] = $category;
    }

    if (!empty($keywords)) {
        $schema['keywords'] = implode(', ', $keywords);
    }

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

/**
 * Author configurations for the site
 */
$authorProfiles = [
    'uk-data-services-team' => [
        'name' => 'UK Data Services Team',
        'role' => 'Data Intelligence Experts',
        'description' => 'Our team of certified data professionals and engineers providing expert guidance on web scraping, data analytics, and business intelligence.'
    ],
    'technical-team' => [
        'name' => 'UK Data Services Technical Team',
        'role' => 'Senior Data Engineers',
        'description' => 'Expert engineers specializing in web scraping technologies, data pipelines, and enterprise data solutions.'
    ],
    'compliance-team' => [
        'name' => 'UK Data Services Compliance Team',
        'role' => 'Data Protection Specialists',
        'description' => 'Specialists in GDPR compliance, data protection, and regulatory requirements for data collection.'
    ]
];

// If $articleData is set, output the schema automatically
if (isset($articleData) && is_array($articleData)) {
    echo generateArticleSchema(
        $articleData['title'],
        $articleData['description'],
        $articleData['datePublished'],
        $articleData['dateModified'] ?? $articleData['datePublished'],
        $articleData['authorName'] ?? 'UK Data Services Team',
        $articleData['imageUrl'],
        $articleData['articleUrl'],
        $articleData['category'] ?? null,
        $articleData['keywords'] ?? []
    );
}
