<?php
define('SECURE_ACCESS', true);
$_SERVER['REQUEST_METHOD'] = 'GET';

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

// Simulate logged-in admin session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
$_SESSION['user_id'] = 1;
$_SESSION['username'] = 'admin';
$_SESSION['role'] = 'admin';

try {
    $_GET['tab'] = 'all';
    $_GET['search'] = 'Tech';
    
    // Disable header sending warning
    if (!function_exists('header')) {
        // Just let it run
    }
    
    ob_start();
    include __DIR__ . '/../api/events_portal.php';
    $output = ob_get_clean();
    
    // Remove warning text from output if any
    $json_start = strpos($output, '{');
    if ($json_start !== false) {
        $output = substr($output, $json_start);
    }
    
    $data = json_decode($output, true);
    if (!$data || !$data['success']) {
        throw new Exception("API request failed or returned invalid response: " . $output);
    }
    
    if (count($data['events']) === 0) {
        throw new Exception("Seeded event search returned 0 items.");
    }
    
    foreach ($data['events'] as $ev) {
        if (strpos(strtolower($ev['title']), 'tech') === false) {
            throw new Exception("Search filter did not correctly match title. Event found: " . $ev['title']);
        }
    }
    
    echo "Event Search API Verification: SUCCESS!\n";
} catch (Exception $e) {
    echo "Event Search API Verification: FAILED: " . $e->getMessage() . "\n";
}
