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

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

$maestro_id = $_POST["maestro_id"];


$maestroInfo = get_maestro_test_player_id($maestro_id);
if($maestroInfo === null) {
	set_error_message("등록된 마에스트로 테스트 계정이 없습니다");
	send_result_fail();
	exit;
}

$maestroTestPlayerInfo = get_maestro_test_player_info($maestroInfo["maestroTestID"]);

set_data("maestroAccountType", $maestroInfo["accountType"]);
set_data("playerID", $maestroInfo["maestroTestID"]);
set_data("playerName", $maestroTestPlayerInfo["Name"]);
set_data("playerAccountType", $maestroTestPlayerInfo["AccountType"]);
send_result_success();
exit;



function get_maestro_test_player_id($maestro_id) {
	global $db_conn;

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

	$result["accountType"] = $accountType;
	$result["maestroTestID"] = $maestroTestID;
	return $result;
}

function get_maestro_test_player_info($player_id) {
	global $db_conn;

	$query = "
		SELECT Name, AccountType
		FROM player
		WHERE PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $player_id);
	$stmt->execute();
	$stmt->bind_result($name, $accountType);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();

	$result["Name"] = $name;
	$result["AccountType"] = $accountType;
	return $result;
}

?>