<?php
/**
 * Meta Tags Helper
 * Generates optimized meta tags for SEO
 *
 * Usage:
 * $metaData = [
 *     'title' => 'Page Title',
 *     'description' => 'Page description...',
 *     'canonicalUrl' => 'https://ukdataservices.co.uk/page',
 *     'ogImage' => '/assets/images/og-image.jpg',
 *     'type' => 'website', // or 'article'
 *     'articleData' => [...] // optional for articles
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/meta-tags.php');
 */

/**
 * Generate complete meta tags for a page
 *
 * @param string $title Page title (will append brand if not already included)
 * @param string $description Meta description (max 160 chars recommended)
 * @param string $canonicalUrl Canonical URL for the page
 * @param string|null $ogImage Open Graph image URL
 * @param array|null $articleData Article-specific data for blog posts
 * @param string $type Page type (website, article, service)
 * @return string HTML meta tags
 */
function generateMetaTags($title, $description, $canonicalUrl, $ogImage = null, $articleData = null, $type = 'website') {
    $baseUrl = 'https://ukdataservices.co.uk';
    $siteName = 'UK Data Services';
    $defaultImage = $baseUrl . '/assets/images/ukds-main-logo.png';
    $twitterHandle = '@ukdataservices';

    // Ensure title includes brand name
    $fullTitle = $title;
    if (strpos(strtolower($title), 'uk data services') === false) {
        $fullTitle = $title . ' | UK Data Services';
    }

    // Truncate description if too long
    if (strlen($description) > 160) {
        $description = substr($description, 0, 157) . '...';
    }

    // Use provided image or default
    $imageUrl = $ogImage ? (strpos($ogImage, 'http') === 0 ? $ogImage : $baseUrl . $ogImage) : $defaultImage;

    $output = '';

    // Basic meta tags
    $output .= '<title>' . htmlspecialchars($fullTitle) . '</title>' . "\n";
    $output .= '    <meta name="description" content="' . htmlspecialchars($description) . '">' . "\n";
    $output .= '    <meta name="author" content="UK Data Services">' . "\n";
    $output .= '    <meta name="robots" content="index, follow">' . "\n";
    $output .= '    <meta name="googlebot" content="index, follow">' . "\n";
    $output .= '    <link rel="canonical" href="' . htmlspecialchars($canonicalUrl) . '">' . "\n";

    // Open Graph tags
    $output .= "\n    <!-- Open Graph / Facebook -->\n";
    $output .= '    <meta property="og:type" content="' . ($type === 'article' ? 'article' : 'website') . '">' . "\n";
    $output .= '    <meta property="og:url" content="' . htmlspecialchars($canonicalUrl) . '">' . "\n";
    $output .= '    <meta property="og:title" content="' . htmlspecialchars($title) . '">' . "\n";
    $output .= '    <meta property="og:description" content="' . htmlspecialchars($description) . '">' . "\n";
    $output .= '    <meta property="og:image" content="' . htmlspecialchars($imageUrl) . '">' . "\n";
    $output .= '    <meta property="og:image:width" content="1200">' . "\n";
    $output .= '    <meta property="og:image:height" content="630">' . "\n";
    $output .= '    <meta property="og:site_name" content="' . $siteName . '">' . "\n";
    $output .= '    <meta property="og:locale" content="en_GB">' . "\n";

    // Twitter Card tags
    $output .= "\n    <!-- Twitter Card -->\n";
    $output .= '    <meta name="twitter:card" content="summary_large_image">' . "\n";
    $output .= '    <meta name="twitter:site" content="' . $twitterHandle . '">' . "\n";
    $output .= '    <meta name="twitter:url" content="' . htmlspecialchars($canonicalUrl) . '">' . "\n";
    $output .= '    <meta name="twitter:title" content="' . htmlspecialchars($title) . '">' . "\n";
    $output .= '    <meta name="twitter:description" content="' . htmlspecialchars($description) . '">' . "\n";
    $output .= '    <meta name="twitter:image" content="' . htmlspecialchars($imageUrl) . '">' . "\n";

    // Article-specific tags
    if ($articleData && $type === 'article') {
        $output .= "\n    <!-- Article Meta -->\n";
        if (isset($articleData['datePublished'])) {
            $output .= '    <meta property="article:published_time" content="' . $articleData['datePublished'] . 'T09:00:00+00:00">' . "\n";
        }
        if (isset($articleData['dateModified'])) {
            $output .= '    <meta property="article:modified_time" content="' . $articleData['dateModified'] . 'T09:00:00+00:00">' . "\n";
        }
        if (isset($articleData['author'])) {
            $output .= '    <meta property="article:author" content="' . htmlspecialchars($articleData['author']) . '">' . "\n";
        }
        if (isset($articleData['section'])) {
            $output .= '    <meta property="article:section" content="' . htmlspecialchars($articleData['section']) . '">' . "\n";
        }
        if (isset($articleData['tags']) && is_array($articleData['tags'])) {
            foreach ($articleData['tags'] as $tag) {
                $output .= '    <meta property="article:tag" content="' . htmlspecialchars($tag) . '">' . "\n";
            }
        }
    }

    return $output;
}

/**
 * Generate geo meta tags for location pages
 */
function generateGeoMetaTags($city, $region, $latitude, $longitude) {
    $output = "\n    <!-- Geo Meta Tags -->\n";
    $output .= '    <meta name="geo.region" content="GB">' . "\n";
    $output .= '    <meta name="geo.placename" content="' . htmlspecialchars($city) . '">' . "\n";
    $output .= '    <meta name="geo.position" content="' . $latitude . ';' . $longitude . '">' . "\n";
    $output .= '    <meta name="ICBM" content="' . $latitude . ', ' . $longitude . '">' . "\n";
    return $output;
}

/**
 * Generate hreflang tags (for potential future internationalization)
 */
function generateHreflangTags($canonicalUrl) {
    $output = "\n    <!-- Hreflang -->\n";
    $output .= '    <link rel="alternate" hreflang="en-GB" href="' . htmlspecialchars($canonicalUrl) . '">' . "\n";
    $output .= '    <link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($canonicalUrl) . '">' . "\n";
    return $output;
}

/**
 * Output standard favicon and manifest links
 */
function generateFaviconTags() {
    $output = "\n    <!-- Favicon and App Icons -->\n";
    $output .= '    <link rel="icon" type="image/svg+xml" href="/assets/images/favicon.svg">' . "\n";
    $output .= '    <link rel="icon" type="image/svg+xml" sizes="16x16" href="/assets/images/favicon-16x16.svg">' . "\n";
    $output .= '    <link rel="icon" type="image/svg+xml" sizes="32x32" href="/assets/images/favicon-32x32.svg">' . "\n";
    $output .= '    <link rel="apple-touch-icon" sizes="180x180" href="/assets/images/apple-touch-icon.svg">' . "\n";
    $output .= '    <link rel="manifest" href="/manifest.json">' . "\n";
    $output .= '    <meta name="theme-color" content="#144784">' . "\n";
    $output .= '    <meta name="msapplication-TileColor" content="#179e83">' . "\n";
    return $output;
}

/**
 * Generate preconnect/prefetch hints for performance
 */
function generateResourceHints() {
    $output = "\n    <!-- Resource Hints -->\n";
    $output .= '    <link rel="preconnect" href="https://fonts.googleapis.com">' . "\n";
    $output .= '    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>' . "\n";
    $output .= '    <link rel="dns-prefetch" href="https://www.google-analytics.com">' . "\n";
    $output .= '    <link rel="dns-prefetch" href="https://www.googletagmanager.com">' . "\n";
    return $output;
}

// If $metaData is set, output meta tags automatically
if (isset($metaData) && is_array($metaData)) {
    echo generateMetaTags(
        $metaData['title'],
        $metaData['description'],
        $metaData['canonicalUrl'],
        $metaData['ogImage'] ?? null,
        $metaData['articleData'] ?? null,
        $metaData['type'] ?? 'website'
    );
}
