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

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

$page_title = $search_query
    ? "Search Results for '" . strip_tags($search_query) . "' | UK AI Automation Blog"
    : "Search | UK AI Automation Blog";
$page_description = "Search our guides on AI automation for UK law firms and management consultancies.";
$canonical_url = "https://ukaiautomation.co.uk/blog/search";

include($_SERVER['DOCUMENT_ROOT'] . '/includes/meta-tags.php');

$articles = [
    [
        'title'    => 'How Law Firms Can Automate Due Diligence Document Review',
        'url'      => '/blog/articles/due-diligence-automation-law-firms',
        'category' => 'Legal Tech',
        'date'     => '2026-03-21',
        'excerpt'  => 'Due diligence is one of the most document-heavy tasks in legal practice. AI extraction systems can now handle the bulk of this work.',
        'keywords' => 'due diligence automation law firm document review AI legal',
    ],
    [
        'title'    => 'Research Automation for Management Consultancies',
        'url'      => '/blog/articles/research-automation-management-consultancy',
        'category' => 'Consultancy Tech',
        'date'     => '2026-03-21',
        'excerpt'  => 'Junior analysts at consultancy firms spend a disproportionate amount of time on desk research that could be largely automated.',
        'keywords' => 'research automation management consultancy desk research market intelligence',
    ],
    [
        'title'    => 'What Is an AI Agent? A Plain-English Guide for Legal and Consultancy Firms',
        'url'      => '/blog/articles/what-is-an-ai-agent-professional-services',
        'category' => 'AI Automation',
        'date'     => '2026-03-21',
        'excerpt'  => 'The term AI agent gets used a lot, but what does it actually mean for a law firm or consultancy?',
        'keywords' => 'AI agent professional services what is an AI agent law firm consultancy',
    ],
    [
        'title'    => 'Document Extraction: From Unstructured PDF to Structured Database',
        'url'      => '/blog/articles/document-extraction-pdf-to-database',
        'category' => 'AI Automation',
        'date'     => '2026-03-21',
        'excerpt'  => 'Modern AI extraction pipelines can turn stacks of PDFs and Word documents into clean, queryable data.',
        'keywords' => 'document extraction PDF database AI pipeline OCR',
    ],
    [
        'title'    => 'The Real Cost of Manual Data Work in Legal and Consultancy Firms',
        'url'      => '/blog/articles/cost-of-manual-data-work-professional-services',
        'category' => 'Business Case',
        'date'     => '2026-03-21',
        'excerpt'  => 'Manual data work costs professional services firms far more than they typically account for.',
        'keywords' => 'manual data work cost ROI automation professional services efficiency',
    ],
    [
        'title'    => 'GDPR and AI Automation: What UK Professional Services Firms Need to Know',
        'url'      => '/blog/articles/gdpr-ai-automation-uk-firms',
        'category' => 'Compliance',
        'date'     => '2026-03-21',
        'excerpt'  => 'GDPR compliance is a legitimate concern when deploying AI automation in UK legal and consultancy firms.',
        'keywords' => 'GDPR AI automation UK data protection compliance',
    ],
];

$search_results = [];
if ($search_query) {
    foreach ($articles as $a) {
        $haystack = strtolower($a['title'] . ' ' . $a['excerpt'] . ' ' . $a['keywords'] . ' ' . $a['category']);
        if (strpos($haystack, strtolower($search_query)) !== false) {
            $search_results[] = $a;
        }
    }
}
?>
<!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="noindex, follow">
    <link rel="canonical" href="<?php echo htmlspecialchars($canonical_url); ?>">
    <link rel="icon" type="image/svg+xml" href="/assets/images/favicon.svg">
    <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">
    <link rel="stylesheet" href="/assets/css/main.css?v=20260321">
</head>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/nav.php'); ?>
<main>
    <section class="page-hero">
        <div class="container">
            <h1>Search</h1>
            <form method="GET" action="">
                <input type="search" name="q" value="<?php echo htmlspecialchars($search_query); ?>"
                       placeholder="Search articles..." aria-label="Search blog articles">
                <button type="submit">Search</button>
            </form>
        </div>
    </section>

    <section class="blog-listing">
        <div class="container">
            <?php if ($search_query): ?>
                <p><?php echo count($search_results); ?> result<?php echo count($search_results) !== 1 ? 's' : ''; ?> for &ldquo;<?php echo htmlspecialchars($search_query); ?>&rdquo;</p>
                <?php if (!empty($search_results)): ?>
                    <div class="articles-grid">
                        <?php foreach ($search_results as $a): ?>
                        <article class="article-card">
                            <div class="article-card-meta">
                                <span class="category"><?php echo htmlspecialchars($a['category']); ?></span>
                            </div>
                            <h2><a href="<?php echo htmlspecialchars($a['url']); ?>"><?php echo htmlspecialchars($a['title']); ?></a></h2>
                            <p><?php echo htmlspecialchars($a['excerpt']); ?></p>
                            <a href="<?php echo htmlspecialchars($a['url']); ?>" class="read-more">Read article &rarr;</a>
                        </article>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <p>No articles found. <a href="/blog/">Browse all articles</a>.</p>
                <?php endif; ?>
            <?php else: ?>
                <p><a href="/blog/">Browse all articles &rarr;</a></p>
            <?php endif; ?>
        </div>
    </section>
</main>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php'); ?>
</body>
</html>
