<?php
/**
 * Internal Linking Strategy Helper
 * Manages internal links across the website
 * Place in: /helpers/internal_linking_helper.php
 */

class InternalLinkingHelper {
    
    private static $links = [];
    
    /**
     * Add internal link with context
     */
    public static function addLink($url, $text, $context = 'content', $keyword = null) {
        self::$links[] = [
            'url' => htmlspecialchars($url, ENT_QUOTES),
            'text' => htmlspecialchars($text, ENT_QUOTES),
            'context' => $context,
            'keyword' => $keyword
        ];
    }
    
    /**
     * Generate internal link HTML
     */
    public static function generateLink($url, $text, $title = null, $class = '') {
        $titleAttr = $title ? ' title="' . htmlspecialchars($title, ENT_QUOTES) . '"' : '';
        $classAttr = $class ? ' class="' . htmlspecialchars($class) . '"' : '';
        
        return '<a href="' . htmlspecialchars($url, ENT_QUOTES) . '"' . $titleAttr . $classAttr . '>' . 
               htmlspecialchars($text) . '</a>';
    }
    
    /**
     * Get related product links
     */
    public static function getRelatedProductLinks($currentProductId, $relatedProducts, $limit = 5) {
        $links = [];
        $count = 0;
        
        foreach ($relatedProducts as $product) {
            if ($count >= $limit) break;
            
            $links[] = [
                'url' => '/product.php?id=' . urlencode($product['id']),
                'text' => htmlspecialchars($product['title']),
                'title' => htmlspecialchars($product['title']) . ' - shop now',
                'class' => 'related-product-link'
            ];
            $count++;
        }
        
        return $links;
    }
    
    /**
     * Get category navigation links
     */
    public static function getCategoryLinks($categories, $currentCategoryId = null) {
        $links = [];
        
        foreach ($categories as $category) {
            $isActive = ($currentCategoryId && $category['id'] == $currentCategoryId);
            
            $links[] = [
                'url' => '/category.php?id=' . urlencode($category['id']),
                'text' => htmlspecialchars($category['name']),
                'title' => 'Browse ' . htmlspecialchars($category['name']),
                'class' => $isActive ? 'category-link active' : 'category-link'
            ];
        }
        
        return $links;
    }
    
    /**
     * Generate breadcrumb navigation
     */
    public static function generateBreadcrumb($items = []) {
        if (empty($items)) {
            $items = [['name' => 'Home', 'url' => '/']];
        }
        
        $html = '<nav class="breadcrumb-nav" aria-label="breadcrumb">' . "\n";
        $html .= '<ol class="breadcrumb">' . "\n";
        
        foreach ($items as $index => $item) {
            $isLast = ($index === count($items) - 1);
            $itemName = isset($item['name']) ? htmlspecialchars($item['name']) : '';
            $itemUrl = isset($item['url']) ? htmlspecialchars($item['url'], ENT_QUOTES) : '#';
            
            if ($isLast) {
                $html .= '<li class="breadcrumb-item active" aria-current="page">' . 
                         $itemName . '</li>' . "\n";
            } else {
                $html .= '<li class="breadcrumb-item">' .
                         '<a href="' . $itemUrl . '">' .
                         $itemName . '</a></li>' . "\n";
            }
        }
        
        $html .= '</ol>' . "\n";
        $html .= '</nav>' . "\n";
        
        return $html;
    }
    
    /**
     * Generate breadcrumb schema
     */
    public static function generateBreadcrumbSchema($items = []) {
        if (empty($items)) {
            return '';
        }
        
        $itemListElement = [];
        foreach ($items as $index => $item) {
            if (isset($item['name']) && isset($item['url'])) {
                $itemListElement[] = [
                    '@type' => 'ListItem',
                    'position' => $index + 1,
                    'name' => $item['name'],
                    'item' => $item['url']
                ];
            }
        }
        
        $schema = [
            '@context' => 'https://schema.org',
            '@type' => 'BreadcrumbList',
            'itemListElement' => $itemListElement
        ];
        
        return '<script type="application/ld+json">' . "\n" .
               json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . "\n" .
               '</script>' . "\n";
    }
    
    /**
     * Generate related posts section
     */
    public static function getRelatedPosts($currentPostId, $relatedPosts, $limit = 3) {
        $links = [];
        $count = 0;
        
        foreach ($relatedPosts as $post) {
            if ($count >= $limit) break;
            if (isset($post['id']) && $post['id'] == $currentPostId) continue;
            
            $links[] = [
                'url' => '/blog.php?slug=' . urlencode($post['slug'] ?? $post['title']),
                'text' => htmlspecialchars($post['title']),
                'title' => 'Read: ' . htmlspecialchars($post['title']),
                'class' => 'related-post-link',
                'excerpt' => isset($post['excerpt']) ? htmlspecialchars(substr($post['excerpt'], 0, 100)) . '...' : ''
            ];
            $count++;
        }
        
        return $links;
    }
    
    /**
     * Generate footer links for SEO
     */
    public static function getFooterLinks($categories, $pages = []) {
        $footerLinks = [
            'categories' => $categories,
            'pages' => $pages ?: [
                ['name' => 'Home', 'url' => '/'],
                ['name' => 'Shop', 'url' => '/shop.php'],
                ['name' => 'Blog', 'url' => '/blogs.php'],
                ['name' => 'About Us', 'url' => '/about.php'],
                ['name' => 'Contact', 'url' => '/contact.php']
            ]
        ];
        
        return $footerLinks;
    }
    
    /**
     * Get all internal links added
     */
    public static function getAll() {
        return self::$links;
    }
    
    /**
     * Clear links
     */
    public static function clear() {
        self::$links = [];
    }
}

/**
 * Helper function for quick linking
 */
function internalLink($url, $text, $title = null, $class = '') {
    return InternalLinkingHelper::generateLink($url, $text, $title, $class);
}
?>
