<?php
// Enhanced security headers
header('Strict-Transport-Security: max-age=31536000; includeSubDomains');

// Get search query
$search_query = isset($_GET['q']) ? trim($_GET['q']) : '';
$search_query = htmlspecialchars($search_query, ENT_QUOTES, 'UTF-8');

// SEO and performance optimizations
$page_title = $search_query ? "Search Results for '{$search_query}' | UK Data Services Blog" : "Search | UK Data Services Blog";
$page_description = $search_query ? "Search results for '{$search_query}' in our data intelligence blog." : "Search our expert insights on web scraping, data analytics, and business intelligence.";
$canonical_url = "https://ukdataservices.co.uk/blog/search.php" . ($search_query ? "?q=" . urlencode($search_query) : "");

// Define all articles with their content for searching
$articles = [
    [
        'title' => 'Complete Guide to Web Scraping Compliance in the UK',
        'url' => 'articles/web-scraping-compliance-uk-guide.php',
        'category' => 'Legal & Compliance',
        'date' => '2025-06-08',
        'excerpt' => 'Navigate the complex landscape of UK data protection laws and ensure your web scraping activities remain fully compliant with GDPR and industry regulations.',
        'read_time' => '12 min read',
        'keywords' => 'web scraping compliance UK GDPR data protection legal regulations'
    ],
    [
        'title' => 'Measuring ROI from Competitive Intelligence Programmes',
        'url' => 'articles/competitive-intelligence-roi-metrics.php',
        'category' => 'Business Intelligence',
        'date' => '2025-06-05',
        'excerpt' => 'Learn how to quantify the business value of competitive intelligence initiatives and demonstrate measurable returns on your data investment.',
        'read_time' => '8 min read',
        'keywords' => 'competitive intelligence ROI metrics business value data investment'
    ],
    [
        'title' => 'Advanced Price Monitoring Strategies for UK Retailers',
        'url' => 'articles/retail-price-monitoring-strategies.php',
        'category' => 'Industry Insights',
        'date' => '2025-06-03',
        'excerpt' => 'Discover how leading British retailers leverage automated price monitoring to maintain competitive advantage and optimise pricing strategies.',
        'read_time' => '10 min read',
        'keywords' => 'price monitoring retail UK competitive pricing strategies automation'
    ],
    [
        'title' => 'Scraping JavaScript-Heavy Sites: Advanced Techniques',
        'url' => 'articles/javascript-heavy-sites-scraping.php',
        'category' => 'Web Scraping',
        'date' => '2025-06-01',
        'excerpt' => 'Master the challenges of extracting data from dynamic websites using modern browser automation and rendering techniques.',
        'read_time' => '6 min read',
        'keywords' => 'javascript scraping dynamic websites browser automation rendering'
    ],
    [
        'title' => 'Building Robust Data Quality Validation Pipelines',
        'url' => 'articles/data-quality-validation-pipelines.php',
        'category' => 'Data Analytics',
        'date' => '2025-05-29',
        'excerpt' => 'Implement comprehensive data validation systems to ensure accuracy and reliability in your data processing workflows.',
        'read_time' => '9 min read',
        'keywords' => 'data quality validation pipelines accuracy reliability workflows'
    ],
    [
        'title' => 'Financial Services Data Transformation Success Story',
        'url' => 'articles/financial-services-data-transformation.php',
        'category' => 'Case Studies',
        'date' => '2025-05-27',
        'excerpt' => 'How a leading UK investment firm automated their market data collection and reduced analysis time by 75%.',
        'read_time' => '7 min read',
        'keywords' => 'financial services data transformation automation market data case study'
    ],
    [
        'title' => 'Cloud-Native Scraping Architecture for Enterprise Scale',
        'url' => 'articles/cloud-native-scraping-architecture.php',
        'category' => 'Technology',
        'date' => '2025-05-25',
        'excerpt' => 'Design scalable, resilient web scraping infrastructure using modern cloud technologies and containerization.',
        'read_time' => '11 min read',
        'keywords' => 'cloud native scraping architecture enterprise scalable infrastructure'
    ],
    [
        'title' => 'UK Property Market: Data-Driven Investment Insights',
        'url' => 'articles/uk-property-market-data-trends.php',
        'category' => 'Industry Insights',
        'date' => '2025-05-22',
        'excerpt' => 'Leverage comprehensive property data analysis to identify emerging investment opportunities across UK markets.',
        'read_time' => '8 min read',
        'keywords' => 'UK property market data investment insights analysis opportunities'
    ],
    [
        'title' => 'GDPR Data Minimisation: Best Practices for Data Teams',
        'url' => 'articles/gdpr-data-minimisation-practices.php',
        'category' => 'Legal & Compliance',
        'date' => '2025-05-20',
        'excerpt' => 'Implement effective data minimisation strategies that comply with GDPR requirements while maintaining analytical value.',
        'read_time' => '6 min read',
        'keywords' => 'GDPR data minimisation compliance best practices analytical value'
    ],
    [
        'title' => 'Python Data Pipeline Tools for 2025',
        'url' => 'articles/python-data-pipeline-tools-2025.php',
        'category' => 'Technology',
        'date' => '2025-05-23',
        'excerpt' => 'Comprehensive guide to the latest Python tools and frameworks for building robust data pipelines in enterprise environments.',
        'read_time' => '11 min read',
        'keywords' => 'python data pipeline tools frameworks enterprise 2025'
    ],
    [
        'title' => 'Professional Rate Limiting Strategies for Web Scraping',
        'url' => 'articles/web-scraping-rate-limiting.php',
        'category' => 'Web Scraping',
        'date' => '2025-05-20',
        'excerpt' => 'Master advanced rate limiting techniques to ensure respectful and sustainable web scraping operations.',
        'read_time' => '9 min read',
        'keywords' => 'rate limiting web scraping professional sustainable techniques'
    ],
    [
        'title' => 'Kubernetes Deployment for Enterprise Scraping',
        'url' => 'articles/kubernetes-scraping-deployment.php',
        'category' => 'Technology',
        'date' => '2025-05-18',
        'excerpt' => 'Deploy and scale web scraping applications using Kubernetes with best practices for production environments.',
        'read_time' => '13 min read',
        'keywords' => 'kubernetes deployment enterprise scraping scaling production'
    ],
    [
        'title' => 'UK Cookie Law Compliance for Data Collection',
        'url' => 'articles/uk-cookie-law-compliance.php',
        'category' => 'Legal & Compliance',
        'date' => '2025-05-15',
        'excerpt' => 'Navigate UK cookie regulations and ensure compliant data collection practices in your web applications.',
        'read_time' => '7 min read',
        'keywords' => 'UK cookie law compliance data collection regulations applications'
    ],
    [
        'title' => 'UK E-commerce Data Trends for 2025',
        'url' => 'articles/ecommerce-trends-uk-2025.php',
        'category' => 'Industry Insights',
        'date' => '2025-05-12',
        'excerpt' => 'Essential insights into the UK e-commerce market using comprehensive data analysis and market intelligence.',
        'read_time' => '10 min read',
        'keywords' => 'UK ecommerce trends 2025 market intelligence data analysis'
    ],
    [
        'title' => 'Healthcare Research Data Collection Success',
        'url' => 'articles/healthcare-research-data-collection.php',
        'category' => 'Case Studies',
        'date' => '2025-05-10',
        'excerpt' => 'How a UK research institution improved data collection efficiency by 60% using automated web scraping solutions.',
        'read_time' => '8 min read',
        'keywords' => 'healthcare research data collection automation efficiency case study'
    ]
];

// Perform search if query exists
$search_results = [];
if ($search_query) {
    foreach ($articles as $article) {
        $search_text = strtolower($article['title'] . ' ' . $article['excerpt'] . ' ' . $article['keywords'] . ' ' . $article['category']);
        $query_lower = strtolower($search_query);
        
        if (strpos($search_text, $query_lower) !== false) {
            $search_results[] = $article;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($page_title); ?></title>
    <meta name="description" content="<?php echo htmlspecialchars($page_description); ?>">
    <meta name="robots" content="index, follow">
    <link rel="canonical" href="<?php echo htmlspecialchars($canonical_url); ?>">
    
    <!-- Preload critical resources -->
    <link rel="preload" href="../assets/css/main.css?v=20260222" as="style">
    <link rel="preload" href="../assets/images/ukds-main-logo.png" as="image">
    
    <!-- Open Graph / Social Media -->
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?php echo htmlspecialchars($canonical_url); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($page_title); ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars($page_description); ?>">
    
    <!-- Favicon -->
    <link rel="icon" type="image/svg+xml" href="../assets/images/favicon.svg">
    
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@300;400;500;600;700&family=Lato:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Styles -->
    <link rel="stylesheet" href="../assets/css/main.css?v=20260222">
</head>
<body>
    <!-- Skip to content link for accessibility -->
    <a href="#main-content" class="skip-to-content">Skip to main content</a>
    
    <!-- Navigation -->
    <?php include($_SERVER["DOCUMENT_ROOT"] . "/includes/nav.php"); ?>

    <!-- Breadcrumb Navigation -->
    <div class="breadcrumb">
        <nav aria-label="Breadcrumb">
            <ol>
                <li><a href="../">Home</a></li>
                <li><a href="../blog/">Blog</a></li>
                <li aria-current="page"><span>Search<?php if ($search_query) echo ': ' . $search_query; ?></span></li>
            </ol>
        </nav>
    </div>

    <!-- Main Content -->
    <main id="main-content">
        <section class="page-hero">
            <div class="container">
                <div class="hero-content">
                    <h1>Search Results</h1>
                    <?php if ($search_query): ?>
                        <p class="hero-subtitle">Found <?php echo count($search_results); ?> article<?php echo count($search_results) !== 1 ? 's' : ''; ?> for "<?php echo htmlspecialchars($search_query); ?>"</p>
                    <?php else: ?>
                        <p class="hero-subtitle">Enter a search term to find relevant articles</p>
                    <?php endif; ?>
                    
                    <!-- Search form -->
                    <div class="hero-search">
                        <form method="GET" action="">
                            <input type="search" name="q" value="<?php echo htmlspecialchars($search_query); ?>" placeholder="Search articles..." aria-label="Search blog articles" required>
                            <button type="submit" aria-label="Search">🔍</button>
                        </form>
                    </div>
                </div>
            </div>
        </section>

        <?php if ($search_query): ?>
            <?php if (!empty($search_results)): ?>
                <!-- Search Results -->
                <section class="blog-recent">
                    <div class="container">
                        <h2>Search Results</h2>
                        <div class="articles-grid">
                            <?php foreach ($search_results as $article): ?>
                                <article class="article-card">
                                    <div class="article-meta">
                                        <span class="category"><?php echo htmlspecialchars($article['category']); ?></span>
                                        <time datetime="<?php echo $article['date']; ?>"><?php echo date('j F Y', strtotime($article['date'])); ?></time>
                                    </div>
                                    <h3><a href="<?php echo htmlspecialchars($article['url']); ?>"><?php echo htmlspecialchars($article['title']); ?></a></h3>
                                    <p><?php echo htmlspecialchars($article['excerpt']); ?></p>
                                    <div class="article-footer">
                                        <span class="read-time"><?php echo htmlspecialchars($article['read_time']); ?></span>
                                        <a href="<?php echo htmlspecialchars($article['url']); ?>" class="read-more">Read →</a>
                                    </div>
                                </article>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </section>
            <?php else: ?>
                <!-- No Results -->
                <section class="blog-recent">
                    <div class="container">
                        <div class="no-results">
                            <h2>No Results Found</h2>
                            <p>We couldn't find any articles matching "<strong><?php echo htmlspecialchars($search_query); ?></strong>".</p>
                            <p>Try searching with different keywords or browse our <a href="../blog/">latest articles</a>.</p>
                            
                            <h3>Popular Topics:</h3>
                            <div class="popular-topics">
                                <a href="?q=web+scraping" class="topic-tag">Web Scraping</a>
                                <a href="?q=data+analytics" class="topic-tag">Data Analytics</a>
                                <a href="?q=GDPR+compliance" class="topic-tag">GDPR Compliance</a>
                                <a href="?q=business+intelligence" class="topic-tag">Business Intelligence</a>
                                <a href="?q=python" class="topic-tag">Python</a>
                                <a href="?q=UK+data" class="topic-tag">UK Data</a>
                            </div>
                        </div>
                    </div>
                </section>
            <?php endif; ?>
        <?php else: ?>
            <!-- Popular Categories -->
            <section class="blog-categories">
                <div class="container">
                    <h2>Browse by Category</h2>
                    <div class="categories-grid">
                        <a href="/blog/categories/web-scraping" class="category-card">
                            <span class="category-icon">🕷️</span>
                            <h3>Web Scraping</h3>
                            <p>Techniques, tools, and best practices</p>
                        </a>
                        
                        <a href="/blog/categories/data-analytics" class="category-card">
                            <span class="category-icon">📊</span>
                            <h3>Data Analytics</h3>
                            <p>Business intelligence insights</p>
                        </a>
                        
                        <a href="/blog/categories/compliance" class="category-card">
                            <span class="category-icon">⚖️</span>
                            <h3>Legal & Compliance</h3>
                            <p>UK data protection and GDPR</p>
                        </a>
                        
                        <a href="/blog/categories/technology" class="category-card">
                            <span class="category-icon">⚙️</span>
                            <h3>Technology</h3>
                            <p>Latest tools and platforms</p>
                        </a>
                    </div>
                </div>
            </section>
        <?php endif; ?>
    </main>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
            <div class="footer-content">
                <div class="footer-section">
                    <div class="footer-logo">
                        <img src="../assets/images/logo-white.svg" alt="UK Data Services" loading="lazy">
                    </div>
                    <p>Enterprise data intelligence solutions for modern British business.</p>
                </div>
                
                <div class="footer-section">
                    <h3>Our Services</h3>
                    <ul>
                        <li><a href="../services/competitive-intelligence">Competitive Intelligence</a></li>
                        <li><a href="../services/price-monitoring">Price Monitoring</a></li>
                        <li><a href="../services/data-cleaning">Data Cleaning</a></li>
                        <li><a href="../#services">All Services</a></li>
                    </ul>
                </div>

                <div class="footer-section">
                    <h3>Locations</h3>
                    <ul>
                        <li><a href="../locations/london">London</a></li>
                        <li><a href="../locations/manchester">Manchester</a></li>
                        <li><a href="../locations/birmingham">Birmingham</a></li>
                    </ul>
                </div>

                <div class="footer-section">
                    <h3>Resources</h3>
                    <ul>
                        <li><a href="../blog/">Data Intelligence Blog</a></li>
                        <li><a href="../case-studies/">Case Studies</a></li>
                        <li><a href="../about">About UK Data Services</a></li>
                        <li><a href="../quote">Request Consultation</a></li>
                    </ul>
                </div>
                
                <div class="footer-section">
                    <h3>Legal & Support</h3>
                    <ul>
                        <li><a href="../privacy-policy">Privacy Policy</a></li>
                        <li><a href="../terms-of-service">Terms of Service</a></li>
                        <li><a href="../gdpr-compliance">GDPR Compliance</a></li>
                        <li><a href="../#contact">Contact & Support</a></li>
                    </ul>
                </div>
            </div>
            
            <div class="footer-bottom">
                <p>&copy; <?php echo date('Y'); ?> UK Data Services. All rights reserved.</p>
                <div class="social-links">
                    <a href="https://linkedin.com/company/uk-data-services" aria-label="LinkedIn" target="_blank" rel="noopener noreferrer"><img src="../assets/images/icon-linkedin.svg" alt="LinkedIn"></a>
                    <a href="https://twitter.com/ukdataservices" aria-label="Twitter" target="_blank" rel="noopener noreferrer"><img src="../assets/images/icon-twitter.svg" alt="Twitter"></a>
                </div>
            </div>
        </div>
    </footer>

    <!-- Scripts -->
    <script src="../assets/js/main.js"></script>
    
    <style>
    .no-results {
        text-align: center;
        padding: 60px 20px;
    }
    
    .no-results h2 {
        color: #1a1a1a;
        margin-bottom: 20px;
    }
    
    .no-results p {
        color: #666;
        margin-bottom: 15px;
        font-size: 18px;
    }
    
    .no-results h3 {
        margin: 40px 0 20px 0;
        color: #333;
    }
    
    .popular-topics {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        justify-content: center;
        margin-top: 20px;
    }
    
    .topic-tag {
        background: linear-gradient(135deg, #179e83 0%, #144784 100%);
        color: white;
        padding: 8px 16px;
        border-radius: 20px;
        text-decoration: none;
        font-size: 14px;
        transition: transform 0.2s ease;
    }
    
    .topic-tag:hover {
        transform: translateY(-2px);
        text-decoration: none;
        color: white;
    }
    
    .hero-search form {
        display: flex;
        align-items: center;
        gap: 0;
    }
    
    .hero-search input {
        flex: 1;
    }
    </style>
</body>
</html>