<?php

require_once __DIR__ . '/config.php';

// ----------------------
// SEARCH QUERY
// ----------------------
$searchTerm = trim($_GET['q'] ?? '');

$parkResults = [];
$postResults = [];

// ----------------------
// PAGE META
// ----------------------
$pageTitle = 'Search ThrillSense';
$metaDescription = 'Search ThrillSense for theme park news, guides, features and park pages.';
$canonicalUrl = 'https://thrillsense.com/search';

// ----------------------
// PAGE CSS
// ----------------------
$extra_head = '
    <link rel="stylesheet" href="/styles/park.css">
    <link rel="stylesheet" href="/styles/search.css">
';

if ($searchTerm !== '') {

    $likeTerm = '%' . $searchTerm . '%';

    // ----------------------
    // PARK RESULTS
    // ----------------------
    $parkStmt = $pdo->prepare("
        SELECT 
            id,
            name,
            slug,
            image_path
        FROM parks
        WHERE name LIKE ?
        ORDER BY name ASC
        LIMIT 6
    ");

    $parkStmt->execute([
        $likeTerm
    ]);

    $parkResults = $parkStmt->fetchAll(PDO::FETCH_ASSOC);

    // ----------------------
    // POST RESULTS
    // ----------------------
    $postStmt = $pdo->prepare("
        SELECT 
            id,
            title,
            tagline,
            meta_description,
            slug,
            tags,
            image_path,
            post_location,
            created_at
        FROM posts
        WHERE LOWER(TRIM(status)) = 'published'
        AND post_location IN ('news', 'article', 'guide')
        AND (
            title LIKE ?
            OR COALESCE(tagline, '') LIKE ?
            OR COALESCE(meta_description, '') LIKE ?
            OR COALESCE(tags, '') LIKE ?
        )
        ORDER BY created_at DESC
        LIMIT 30
    ");

    $postStmt->execute([
        $likeTerm,
        $likeTerm,
        $likeTerm,
        $likeTerm
    ]);

    $postResults = $postStmt->fetchAll(PDO::FETCH_ASSOC);
}

$totalResults = count($parkResults) + count($postResults);

require_once __DIR__ . '/includes/header.php';

?>

<main class="news-page ts-search-page">

    <section class="hero-news">
        <div class="hero-text">
            <h1 class="title-alt">Search</h1>
        </div>
    </section>

    <section class="ts-search-control">

        <form action="/search" method="get" class="ts-search-page-form">

            <i class="bi bi-search"></i>

            <input 
                type="search"
                name="q"
                value="<?= htmlspecialchars($searchTerm) ?>"
                placeholder="Search Alton Towers, Universal, Thorpe Park..."
            >

            <button type="submit">
                Search
            </button>

        </form>

    </section>

    <section class="ts-search-results-wrap">

        <?php if ($searchTerm === ''): ?>

            <div class="ts-search-empty-intro">

                <h2>
                    What are you looking for?
                </h2>

                <p>
                    Try searching for a park, ride, event or topic.
                </p>

            </div>

        <?php else: ?>

            <h2 class="ts-search-results-heading">
                Search results for “<?= htmlspecialchars($searchTerm) ?>”
            </h2>

            <p class="ts-search-results-count">
                <?= $totalResults ?> result<?= $totalResults === 1 ? '' : 's' ?> found.
            </p>

            <?php if (!empty($parkResults)): ?>

                <div class="ts-search-section">

                    <h3 class="ts-search-section-title">
                        Park matches
                    </h3>

                    <div class="ts-park-search-results">

                        <?php foreach ($parkResults as $park): ?>

                            <?php
                            $parkUrl = '/park/' . $park['slug'];
                            $parkImage = $park['image_path'] ?: '/uploads/default.webp';

                            if (strpos($parkImage, '/') !== 0) {
                                $parkImage = '/' . $parkImage;
                            }
                            ?>

                            <article class="ts-park-search-card">

                                <a href="<?= htmlspecialchars($parkUrl) ?>">

                                    <div class="ts-park-search-image">

                                        <img 
                                            src="<?= htmlspecialchars($parkImage) ?>"
                                            alt="<?= htmlspecialchars($park['name']) ?>"
                                        >

                                    </div>

                                    <div>

                                        <small class="ts-park-search-label">
                                            Park
                                        </small>

                                        <h3 class="ts-park-search-title">
                                            <?= htmlspecialchars($park['name']) ?>
                                        </h3>

                                        <p class="ts-park-search-description">
                                            View the latest ThrillSense coverage for this park.
                                        </p>

                                    </div>

                                </a>

                            </article>

                        <?php endforeach; ?>

                    </div>

                </div>

            <?php endif; ?>

            <?php if (!empty($postResults)): ?>

                <div class="ts-search-section">

                    <h3 class="ts-search-section-title">
                        News, articles and guides
                    </h3>

                    <div class="ts-search-results">

                        <?php foreach ($postResults as $post): ?>

                            <?php
                            $postType = $post['post_location'] ?: 'article';
                            $postUrl = '/' . $postType . '/' . $post['slug'];

                            $description = $post['tagline'] 
                                ?: $post['meta_description'] 
                                ?: 'Read more on ThrillSense.';

                            $imagePath = $post['image_path'] ?: '/uploads/default.webp';

                            if (strpos($imagePath, '/') !== 0) {
                                $imagePath = '/' . $imagePath;
                            }

                            $label = ucfirst($postType);
                            ?>

                            <article class="ts-search-card">

                                <a href="<?= htmlspecialchars($postUrl) ?>">

                                    <div class="ts-search-card-image">

                                        <img 
                                            src="<?= htmlspecialchars($imagePath) ?>"
                                            alt="<?= htmlspecialchars($post['title']) ?>"
                                        >

                                    </div>

                                    <div class="ts-search-card-body">

                                        <small class="ts-search-card-label">
                                            <?= htmlspecialchars($label) ?>
                                        </small>

                                        <h3 class="ts-search-card-title">
                                            <?= htmlspecialchars($post['title']) ?>
                                        </h3>

                                        <p class="ts-search-card-description">
                                            <?= htmlspecialchars($description) ?>
                                        </p>

                                    </div>

                                </a>

                            </article>

                        <?php endforeach; ?>

                    </div>

                </div>

            <?php endif; ?>

            <?php if (empty($parkResults) && empty($postResults)): ?>

                <div class="ts-search-no-results">

                    <h3>
                        No results found
                    </h3>

                    <p>
                        Try a broader search, such as a park name, ride name or destination.
                    </p>

                </div>

            <?php endif; ?>

        <?php endif; ?>

    </section>

</main>

<?php require_once __DIR__ . '/includes/footer.php'; ?>