<?php

include "lib/json-headers.php";
include "lib/remote.php";

$blob_encoded = remote_query("SELECT nosql FROM radios WHERE website='$dom'")[0]['nosql'];

if (!$blob_encoded) {
    http_response_code(404);
    die('{ "error": "No encontrado" }');
}

/*
 * GET
 *
 * sin parametros
 */

if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    $blob = json_decode($blob_encoded, True); // stdclass object to array

    if(!isset($blob['finished']) || !$blob['finished']) {
        die('{ "message": "No está finalizada aún." }');
    }

    $blob['finished'] = false;

    $blob_encoded = json_encode($blob, JSON_UNESCAPED_UNICODE);

    if (!$blob_encoded) {
        http_response_code(409);
        die('{ "error": "Conflicto al guardar" }');
    }

    remote_query("UPDATE radios SET nosql='$blob_encoded' WHERE website='$dom'");

    die('{ "message": "Finalizada puesto en «no»." }');

}
