<?php
/**
 * GET /api/events — public list for Member PWA (no auth).
 * Optional: ?group=All or group name to filter (future); for now returns all upcoming events.
 */
require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../config/database.php';

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

$today = date('Y-m-d');
try {
    $stmt = $pdo->prepare("
        SELECT e.id, e.title, e.description, e.event_date, e.event_time, e.location,
               et.name AS event_type_name
        FROM events e
        LEFT JOIN event_types et ON et.id = e.event_type_id
        WHERE e.event_date >= ?
        ORDER BY e.event_date ASC, e.event_time ASC
        LIMIT 50
    ");
    $stmt->execute([$today]);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($rows);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([]);
}
