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

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

$maestroID = $_POST["maestro_id"];
$playerID = $_POST["player_id"];


deleteAppHighestRecord($maestroID, $playerID);
deleteBestRecord($maestroID, $playerID);
deleteLicenseScore($maestroID, $playerID);
deleteLicenseTime($maestroID, $playerID);

deletePlayer($maestroID, $playerID);

$countPlayer = countPlayerID($maestroID, $playerID);
if($countPlayer > 0) {
	set_error_message("학생 계정 삭제 실패");
	send_result_fail();
	exit;
}

$countPlayer = countPlayerRecord($maestroID, $playerID);
if($countPlayer > 0) {
	set_error_message("학생 플레이 기록 삭제 실패");
	send_result_fail();
	exit;
}

// decrease_player_count($maestroID, 1);
$count = count_registered_player($maestroID);
update_count_registered_player($maestroID, $count);

set_data("count", $count);
send_result_success();
exit;



function deleteAppHighestRecord($maestroID, $playerID) {
	global $db_conn;

	$query = "
		DELETE FROM app_highest_record
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$result = $stmt->execute();
	$stmt->close();
}

function deleteBestRecord($maestroID, $playerID) {
	global $db_conn;

	$query = "
		DELETE FROM best_record
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$result = $stmt->execute();
	$stmt->close();
}

function deleteLicenseScore($maestroID, $playerID) {
	global $db_conn;

	$query = "
		DELETE FROM license_score
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$result = $stmt->execute();
	$stmt->close();
}

function deleteLicenseTime($maestroID, $playerID) {
	global $db_conn;

	$query = "
		DELETE FROM license_time
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$result = $stmt->execute();
	$stmt->close();
}

function deletePlayer($maestroID, $playerID) {
	global $db_conn;

	$query = "
		DELETE FROM player
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$result = $stmt->execute();
	$stmt->close();

	// if($result) {
	// 	echo "deleted row count : ".$stmt->affected_rows;
	// }
}

function countPlayerID($maestroID, $playerID) {
	global $db_conn;

	$query = "
		SELECT COUNT(PlayerID)
		FROM player
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$stmt->execute();
	$stmt->bind_result($countPlayer);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $countPlayer;
}

function countPlayerRecord($maestroID, $playerID) {
	global $db_conn;

	$query = "
		SELECT COUNT(PlayerID)
		FROM best_record
		WHERE MaestroID = ? AND PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $maestroID, $playerID);
	$stmt->execute();
	$stmt->bind_result($countPlayer);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $countPlayer;
}

function decrease_player_count($maestroID, $count) {
	global $db_conn;

	$query = "
		UPDATE maestro
		SET PlayerCount = PlayerCount - ?
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $count, $maestroID);
	$stmt->execute();
}

function count_registered_player($maestroID) {
	global $db_conn;

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

	return $countPlayer;
}

function update_count_registered_player($maestroID, $countPlayer) {
	global $db_conn;

	$query = "
		UPDATE maestro
		SET PlayerCount = ?
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $countPlayer, $maestroID);
	$stmt->execute();
}

?>