<?php
$datetime = date("Y-m-d H:i:s");
$txt  = "\n$datetime - ".$_SERVER['PHP_SELF']."\n";
$txt .= print_r($user, true);
$txt .= print_r($_POST, true);

//make sure user is superAdmin
if ( !$user['superAdmin'] ) {
$txt .= "403 not superAdmin\n";

    http_response_code(403);
    echo json_encode('You do not have permission to delete models');
    exit;
}

//validate model id
if ( !$model = $db->row("SELECT * FROM model WHERE id = ?", $_POST['modelId']) ) {
$txt .= "400 invalid model ID\n";

    http_response_code(400);
    echo json_encode('Invalid model ID');
    exit;
} 
$txt .= print_r($model, true);

$db->delete('model', ['id' => $_POST['modelId']]);

$txt .= "done\n\n\n\n";
$file = fopen(PATH_LOGS.'/btb.delete-models.log', "a");
fwrite($file, $txt);
fclose($file);

$userProject = $db->row("SELECT * FROM project WHERE id = ?", $model['projectId']);
$models = $db->run("SELECT * FROM model WHERE projectId = ? ORDER BY createdAt DESC", $model['projectId']);
include '../template/models.php';