<?php 
require_once 'config.php';
include 'header.php'; 

$query = isset($_GET['q']) ? trim(htmlspecialchars($_GET['q'])) : '';

// Match pool using the global $MEGA structure
$results = [];

// Stable high-fidelity Unsplash images for categories
$categoryImages = [
    'ai' => 'https://images.unsplash.com/photo-1677442136019-21780efad99a?w=600&auto=format&fit=crop&q=80',
    'agile' => 'https://images.unsplash.com/photo-1531403009284-440f080d1e12?w=600&auto=format&fit=crop&q=80',
    'pm' => 'https://images.unsplash.com/photo-1454165804606-c3d57bc86b40?w=600&auto=format&fit=crop&q=80',
    'cyber' => 'https://images.unsplash.com/photo-1550751827-4bd374c3f58b?w=600&auto=format&fit=crop&q=80',
    'cloud' => 'https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=600&auto=format&fit=crop&q=80',
    'itsm' => 'https://images.unsplash.com/photo-1460925895917-afdab827c52f?w=600&auto=format&fit=crop&q=80',
    'ds' => 'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=600&auto=format&fit=crop&q=80',
    'devops' => 'https://images.unsplash.com/photo-1618401471353-b98aedd07871?w=600&auto=format&fit=crop&q=80',
    'web' => 'https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=600&auto=format&fit=crop&q=80',
];

if (!empty($query)) {
    $searchTerms = array_filter(explode(' ', strtolower($query)));
    
    foreach ($MEGA as $catKey => $catData) {
        $categoryLabel = strtolower($catData['label']);
        
        foreach ($catData['links'] as $link) {
            $courseTitle = $link[0];
            $courseUrl = $link[1];
            $lowerTitle = strtolower($courseTitle);
            
            $score = 0;
            
            // 1. Exact match with title
            if ($lowerTitle === strtolower($query)) {
                $score += 100;
            }
            // 2. Starts with query
            elseif (strpos($lowerTitle, strtolower($query)) === 0) {
                $score += 80;
            }
            // 3. Contains full query string
            elseif (strpos($lowerTitle, strtolower($query)) !== false) {
                $score += 60;
            }
            // 4. Contains part of the search terms
            else {
                $matchesCount = 0;
                foreach ($searchTerms as $term) {
                    if (strpos($lowerTitle, $term) !== false) {
                        $matchesCount++;
                    }
                }
                if ($matchesCount > 0) {
                    $score += ($matchesCount * 15);
                }
            }
            
            // 5. Category label matches
            if (strpos($categoryLabel, strtolower($query)) !== false) {
                $score += 30;
            }
            
            if ($score > 0) {
                // Determine duration
                $duration = '35 Hours';
                if (strpos($lowerTitle, 'scrum') !== false || strpos($lowerTitle, 'csm') !== false || strpos($lowerTitle, 'cspo') !== false) {
                    $duration = '16 Hours';
                } elseif (strpos($lowerTitle, 'itil') !== false) {
                    $duration = '16 Hours';
                } elseif (strpos($lowerTitle, 'bootcamp') !== false || strpos($lowerTitle, 'fullstack') !== false) {
                    $duration = '120 Hours';
                }
                
                // Deterministic enrolled count based on title
                $enrolledHash = (abs(crc32($courseTitle)) % 3000) + 1200;
                $enrolled = number_format($enrolledHash) . ' Enrolled';
                
                $results[] = [
                    'title' => $courseTitle,
                    'url' => BASE_URL . ltrim($courseUrl, '/'),
                    'category' => $catData['label'],
                    'category_url' => BASE_URL . ltrim($catData['browse'], '/'),
                    'image' => $categoryImages[$catKey] ?? 'https://images.unsplash.com/photo-1522202176988-66273c2fd55f?w=600',
                    'duration' => $duration,
                    'enrolled' => $enrolled,
                    'score' => $score
                ];
            }
        }
    }
    
    // Sort results by relevance score (descending)
    usort($results, function($a, $b) {
        return $b['score'] - $a['score'];
    });
}
?>

<!-- Custom Premium CSS Styles for Search Results Page -->
<style>
    .sc-search-bg {
        background-color: #F8FAF9;
        min-height: calc(100vh - 120px);
        padding: 60px 20px;
        box-sizing: border-box;
    }

    .sc-search-container {
        max-width: 1200px;
        margin: 0 auto;
        font-family: 'Inter', system-ui, -apple-system, sans-serif;
    }

    /* Search Header Styling */
    .sc-search-header {
        text-align: center;
        max-width: 700px;
        margin: 0 auto 50px auto;
    }

    .sc-search-header h1 {
        font-size: 38px;
        font-weight: 800;
        color: #2C4765;
        margin: 0 0 10px 0;
        letter-spacing: -0.5px;
        line-height: 1.2;
    }

    .sc-search-header p {
        font-size: 16px;
        color: #64748b;
        margin: 0 0 25px 0;
        line-height: 1.5;
    }

    .sc-search-header .sc-highlight {
        color: #5DA363;
        font-weight: 700;
    }

    /* Premium Search Bar Form */
    .sc-search-box-form {
        display: flex;
        max-width: 480px;
        margin: 0 auto;
        box-shadow: 0 10px 30px rgba(44, 71, 101, 0.08);
        border-radius: 12px;
        overflow: hidden;
        border: 1.5px solid #e2e8f0;
        background: #ffffff;
        transition: all 0.3s ease;
    }

    .sc-search-box-form:focus-within {
        border-color: #5DA363;
        box-shadow: 0 10px 30px rgba(93, 163, 99, 0.15);
        transform: translateY(-1px);
    }

    .sc-search-box-input {
        flex: 1;
        padding: 14px 20px;
        border: none;
        outline: none;
        font-size: 14px;
        color: #1e293b;
        background: transparent;
        font-family: inherit;
        font-weight: 500;
    }

    .sc-search-box-input::placeholder {
        color: #94a3b8;
    }

    .sc-search-box-btn {
        background: #5DA363;
        color: #ffffff;
        border: none;
        padding: 0 28px;
        font-size: 14px;
        font-weight: 700;
        cursor: pointer;
        transition: background 0.2s ease;
        font-family: inherit;
    }

    .sc-search-box-btn:hover {
        background: #4b8a50;
    }

    /* Grid Layout */
    .sc-search-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
        gap: 32px;
        margin-top: 20px;
    }

    /* Premium Card Design */
    .sc-search-card {
        background: #ffffff;
        border-radius: 16px;
        border: 1px solid #e2e8f0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.03), 0 2px 4px -1px rgba(0, 0, 0, 0.02);
        cursor: pointer;
        position: relative;
        text-decoration: none !important;
        color: inherit !important;
    }

    .sc-search-card:hover {
        transform: translateY(-6px);
        box-shadow: 0 20px 25px -5px rgba(44, 71, 101, 0.12), 0 10px 10px -5px rgba(44, 71, 101, 0.04);
        border-color: #5DA363;
    }

    .sc-card-image-wrap {
        height: 200px;
        width: 100%;
        position: relative;
        overflow: hidden;
        background-color: #e2e8f0;
    }

    .sc-card-image-wrap img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.5s ease;
    }

    .sc-search-card:hover .sc-card-image-wrap img {
        transform: scale(1.06);
    }

    .sc-card-badge {
        position: absolute;
        top: 16px;
        left: 16px;
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(4px);
        color: #2C4765;
        font-size: 11px;
        font-weight: 800;
        padding: 6px 12px;
        border-radius: 30px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
    }

    .sc-card-body {
        padding: 24px 24px 16px 24px;
        flex-grow: 1;
        display: flex;
        flex-direction: column;
    }

    .sc-card-title {
        font-size: 18px;
        font-weight: 700;
        color: #1e293b;
        line-height: 1.4;
        margin: 0 0 10px 0;
        transition: color 0.2s ease;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        min-height: 50px;
        text-decoration: none !important;
    }

    .sc-search-card:hover .sc-card-title {
        color: #5DA363;
    }

    .sc-card-desc {
        font-size: 13px;
        color: #64748b;
        line-height: 1.5;
        margin: 0;
    }

    /* Card Footer & CTAs */
    .sc-card-footer {
        padding: 0 24px 24px 24px;
    }

    .sc-card-divider {
        height: 1px;
        background: #f1f5f9;
        margin-bottom: 16px;
    }

    .sc-card-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 12px;
        color: #64748b;
        margin-bottom: 16px;
    }

    .sc-meta-item {
        display: flex;
        align-items: center;
        gap: 6px;
        font-weight: 600;
    }

    .sc-meta-item i {
        color: #5DA363;
        font-size: 13px;
    }

    .sc-card-btn {
        width: 100%;
        display: block;
        text-align: center;
        background: #2C4765;
        color: #ffffff !important;
        font-size: 13px;
        font-weight: 700;
        padding: 12px 0;
        border-radius: 10px;
        transition: all 0.2s ease;
        text-decoration: none !important;
        border: none;
        cursor: pointer;
    }

    .sc-search-card:hover .sc-card-btn {
        background: #5DA363;
    }

    /* No results display state */
    .sc-no-results {
        text-align: center;
        padding: 50px 30px;
        background: #ffffff;
        border-radius: 20px;
        border: 1px solid #e2e8f0;
        max-width: 600px;
        margin: 40px auto 0 auto;
        box-shadow: 0 10px 30px rgba(44, 71, 101, 0.04);
    }

    .sc-no-results-icon {
        font-size: 52px;
        color: #cbd5e1;
        margin-bottom: 20px;
    }

    .sc-no-results h3 {
        font-size: 22px;
        color: #1e293b;
        font-weight: 700;
        margin: 0 0 10px 0;
    }

    .sc-no-results p {
        font-size: 14px;
        color: #64748b;
        margin: 0 0 30px 0;
        line-height: 1.5;
    }

    .sc-rec-title {
        font-size: 11px;
        font-weight: 800;
        text-transform: uppercase;
        color: #94a3b8;
        letter-spacing: 1.5px;
        margin-bottom: 16px;
    }

    .sc-rec-list {
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        gap: 12px;
    }

    .sc-rec-link {
        padding: 8px 16px;
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        font-size: 12px;
        font-weight: 600;
        color: #475569;
        text-decoration: none;
        transition: all 0.2s ease;
    }

    .sc-rec-link:hover {
        background: #F0F9F1;
        border-color: #5DA363;
        color: #5DA363;
    }

    /* Responsive scaling */
    @media (max-width: 768px) {
        .sc-search-grid {
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
        }
        .sc-search-header h1 {
            font-size: 30px;
        }
        .sc-search-bg {
            padding: 40px 15px;
        }
    }
</style>

<div class="sc-search-bg">
    <div class="sc-search-container">
        
        <!-- Search Header Section -->
        <div class="sc-search-header">
            <h1>Search Results</h1>
            <?php if(!empty($query)): ?>
                <p>
                    We found <span class="sc-highlight"><?php echo count($results); ?></span> related courses for <span class="sc-highlight">"<?php echo $query; ?>"</span>
                </p>
            <?php else: ?>
                <p>
                    Find professional certification bootcamps and expert-led training courses.
                </p>
            <?php endif; ?>
            
            <!-- Beautiful On-Page Search input box -->
            <form action="search.php" method="GET" class="sc-search-box-form">
                <input type="text" name="q" value="<?php echo $query; ?>" placeholder="Search another course..." class="sc-search-box-input" required autocomplete="off">
                <button type="submit" class="sc-search-box-btn">
                    Search
                </button>
            </form>
        </div>

        <?php if(!empty($query) && count($results) > 0): ?>
            <!-- Dynamic Search Grid -->
            <div class="sc-search-grid">
                <?php foreach($results as $res): ?>
                    <a href="<?php echo $res['url']; ?>" class="sc-search-card">
                        
                        <!-- Top Half of the Card -->
                        <div>
                            <!-- Card Image Wrapper -->
                            <div class="sc-card-image-wrap">
                                <img alt="<?php echo $res['title']; ?>" loading="lazy" src="<?php echo $res['image']; ?>">
                                <div class="sc-card-badge">
                                    <?php echo $res['category']; ?>
                                </div>
                            </div>

                            <!-- Card Body -->
                            <div class="sc-card-body">
                                <h3 class="sc-card-title">
                                    <?php echo $res['title']; ?>
                                </h3>
                                <p class="sc-card-desc">
                                    Earn standard credentials recognized globally by elite employers.
                                </p>
                            </div>
                        </div>

                        <!-- Card Footer -->
                        <div class="sc-card-footer">
                            <div class="sc-card-divider"></div>
                            
                            <!-- Metadata Grid -->
                            <div class="sc-card-meta">
                                <div class="sc-meta-item">
                                    <i class="fa-regular fa-clock"></i>
                                    <span><?php echo $res['duration']; ?></span>
                                </div>
                                <div class="sc-meta-item">
                                    <i class="fa-solid fa-users"></i>
                                    <span><?php echo $res['enrolled']; ?></span>
                                </div>
                            </div>
                            
                            <!-- CTA Button -->
                            <button class="sc-card-btn">
                                View Course Details
                            </button>
                        </div>

                    </a>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            
            <!-- Elegant No-Results Banner -->
            <div class="sc-no-results">
                <div class="sc-no-results-icon">
                    <i class="fa-solid fa-magnifying-glass"></i>
                </div>
                <h3>No matching courses found</h3>
                <p>
                    <?php if(!empty($query)): ?>
                        We couldn't find any courses matching "<?php echo $query; ?>". Please check your spelling, try different keywords, or explore our top courses below.
                    <?php else: ?>
                        Enter a keyword above to find your desired bootcamps or professional courses.
                    <?php endif; ?>
                </p>
                
                <!-- Curated Recommendations -->
                <div class="sc-rec-box">
                    <h4 class="sc-rec-title">Popular Certifications</h4>
                    <div class="sc-rec-list">
                        <a href="<?php echo BASE_URL; ?>agile-management/csm-certification-training.php" class="sc-rec-link">
                            CSM Certification
                        </a>
                        <a href="<?php echo BASE_URL; ?>project-management/pmp-certification-training.php" class="sc-rec-link">
                            PMP Certification
                        </a>
                        <a href="<?php echo BASE_URL; ?>cloud-computing/aws-solution-architect-certification-training.php" class="sc-rec-link">
                            AWS Solutions Architect
                        </a>
                        <a href="<?php echo BASE_URL; ?>data-science/applied-agentic-ai-certification-course.php" class="sc-rec-link">
                            Applied Agentic AI
                        </a>
                    </div>
                </div>
            </div>

        <?php endif; ?>

    </div>
</div>

<?php include 'footer.php'; ?>
