<?php
header('Content-Type: application/json');
define('SECURE_ACCESS', true);
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/auth.php';

$response = [
    'success' => true,
    'site_name' => 'EventPortal',
    'logo_url' => '',
    'events' => []
];

try {
    $stmt_c = $pdo->query("SELECT key, value FROM system_settings");
    $configs = $stmt_c->fetchAll(PDO::FETCH_KEY_PAIR) ?: [];
    
    $response['site_name'] = $configs['site_name'] ?? 'EventPortal';
    $response['logo_url'] = $configs['site_logo'] ?? '';

    $stmt_ev = $pdo->query("SELECT * FROM events WHERE start_date > NOW() AND status = 'published' ORDER BY start_date ASC LIMIT 3");
    $response['events'] = $stmt_ev->fetchAll(PDO::FETCH_ASSOC) ?: [];
} catch (PDOException $e) {
    $response['success'] = false;
}

echo json_encode($response);
