<?php if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')) {
    require_once('../../../'.$_REQUEST['tenant'].'/config.php');
}
if(file_exists('/config.php')) {
    require_once('/config.php'); 
}


$assignment_id = $_POST['assignment_id'];
$action = $_POST['action'];

$status = ($action === 'started') ? 1 : 2; // 1 = In Progress, 2 = Completed

function markAssignmentColumnExists(mysqli $con, string $column): bool {
    $stmt = $con->prepare("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_assignments' AND COLUMN_NAME = ? LIMIT 1");
    if (!$stmt) return false;
    $stmt->bind_param('s', $column);
    $stmt->execute();
    $stmt->store_result();
    $exists = $stmt->num_rows > 0;
    $stmt->close();
    return $exists;
}

$sql = markAssignmentColumnExists($con, 'updated_at')
    ? "UPDATE admin_assignments SET status = ?, updated_at = UNIX_TIMESTAMP() WHERE id = ?"
    : "UPDATE admin_assignments SET status = ? WHERE id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param('ii', $status, $assignment_id);

if ($stmt->execute()) {
    echo 'success';
} else {
    echo 'error';
}

$stmt->close();
$con->close();
?>
