<?php
header('Content-Type: application/json');

$maestro_id = $_POST["maestro_id"];

include "./../setup/connect_db.php";

$maestro_test_player_id = get_maestro_test_player_id($maestro_id);
if($maestro_test_player_id === null) {
	$replyJSON["ERROR"] = "등록된 마에스트로 테스트 계정이 없습니다";
	echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
	$db_conn->close();
	exit;
}

delete_maestro_test_player_best_record($maestro_test_player_id);
delete_maestro_test_player_app_highest_record($maestro_test_player_id);

$replyJSON["PlayerID"] = $maestro_test_player_id;
$replyJSON["RESULT"] = "ok";
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
exit;


function get_maestro_test_player_id($maestro_id) {
	global $db_conn;

	$query = "
		SELECT MaestroTestID
		FROM maestro
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('i', $maestro_id);
	$stmt->execute();
	$stmt->bind_result($maestro_test_player_id);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();

	return $maestro_test_player_id;
}

function delete_maestro_test_player_best_record($player_id) {
	global $db_conn;

	$query = "
		DELETE
		FROM best_record
		WHERE PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('i', $player_id);
	$stmt->execute();
	// $stmt->bind_result($maestro_test_player_id);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();
}

function delete_maestro_test_player_app_highest_record($player_id) {
	global $db_conn;

	$query = "
		DELETE
		FROM app_highest_record
		WHERE PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('i', $player_id);
	$stmt->execute();
	// $stmt->bind_result($maestro_test_player_id);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();
}

?>