<?php
    include 'db_conn.php';

    $stmt = $db->prepare('SELECT Events.*, username, ifnull(group_concat(label), "Unlabeled") AS labels,
                             ifnull(group_concat(el.label_id), "0") AS label_ids,
                             ifnull(group_concat(color_code), "1976d2") AS clr,
                             ifnull(group_concat(text_color), "1") AS tclr
                          FROM Events
                          JOIN Users USING (user_id)
                          LEFT JOIN `Event_labels` el USING (event_id)
                          LEFT JOIN `Labels` lb ON (el.label_id = lb.label_id)
			  LEFT JOIN `Colors` cl USING (color_id)
                          GROUP BY event_id
                          ORDER BY start_date ASC, event_id ASC;');

    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $json_results = json_encode($results);

    /* Not secure, but it only affects whether our client
     * updates, so it doesn't matter. */
    array_push($results, hash('md5', $json_results));

    echo json_encode($results);
?>
