<?php
// ============================================================
// backend/api/events/EventController.php
// ============================================================

class EventController {

    private PDO $db;

    public function __construct() {
        $this->db = getDB();
    }

    // GET /events  — supports ?status=upcoming|past
    public function index(): void {
        $status = $_GET['status'] ?? null;
        $sql    = 'SELECT * FROM events';
        $params = [];

        if ($status && in_array($status, ['upcoming', 'past'])) {
            $sql .= ' WHERE status = ?';
            $params[] = $status;
        }

        $sql .= ' ORDER BY event_date DESC';
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        sendSuccess($stmt->fetchAll());
    }

    // GET /events/{id}
    public function show(string $id): void {
        $stmt = $this->db->prepare('SELECT * FROM events WHERE id = ?');
        $stmt->execute([$id]);
        $event = $stmt->fetch();
        if (!$event) sendError('Event not found', 404);
        sendSuccess($event);
    }
}
