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

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

$maestroID = $_POST["maestro_id"];


$info = get_maestro_info($maestroID);
if($info === null) {
	set_error_message("마에스트로 계정 정보를 가져올 수 없습니다.");
	send_result_fail();
	exit;
}
$playerCount = getPlayerCount($maestroID);

set_data("name", $info["name"]);
set_data("email", $info["email"]);
set_data("accountType", $info["accountType"]);
set_data("availableActivateDateTime", $info["availableActivateDateTime"]);
set_data("playerCount", $playerCount);
send_result_success();
exit;


function get_maestro_info($maestroID) {
	global $db_conn;

	$query = "
		SELECT Name, Email, AccountType, AvailableActivateDateTime, PlayerCount
		FROM maestro
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("s", $maestroID);
	$stmt->execute();
	$stmt->bind_result($name, $email, $accountType, $availableActivateDateTime, $playerCount);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	$info["name"] = $name;
	$info["email"] = $email;
	$info["accountType"] = $accountType;
	$info["availableActivateDateTime"] = $availableActivateDateTime;
	$info["playerCount"] = $playerCount;

	return $info;
}

function getPlayerCount($maestroID) {
	global $db_conn;

	$query = "
		SELECT COUNT(PlayerID)
		FROM player
		WHERE MaestroID = ? AND AccountType <> 1
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestroID);
	$stmt->execute();
	$stmt->bind_result($playerCount);
	$stmt->fetch();
	$stmt->close();

	return $playerCount;
}

?>