<?php

/**
 * Returns the current user response to the presence question.
 * Works as a list because the id should not be needed for the query.
 */

// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once '../../config/database.php';
include_once '../objects/presence.php';
include_once '../objects/user.php';
include_once '../authenticator.php';

if ($_SERVER['REQUEST_METHOD'] != 'GET') {
    http_response_code(400);
    echo json_encode(array("error" => "Method not accepted."));
    exit();
}

// instantiate database and product object
$database = new Database();
$db = $database->getConnection();

$auth = new Authenticator($db);
$uid = $auth->authenticate();

// query products
$stmt = Presence::read($db, $uid);

$resp=array();
$resp["records"]=array();
$resp["page"]=1;
$resp["more"]=FALSE;

$presence = $stmt->fetch(PDO::FETCH_ASSOC);
if ($presence) {
    $resp["records"][] = [
        'willBePresent' => $presence['will_be_present'] ? true : false,
        'notes' => $presence['notes']
    ];
}

// set response code - 200 OK
http_response_code(200);

// show products data in json format
echo json_encode($resp);
