#include "server.h"
#include <boost/beast/websocket.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <iostream>
#include <sstream>
#include <random>
#include <algorithm>
#include <chrono>

std::vector<std::string> split(const std::string& s, char delimiter) {
	std::vector<std::string> tokens;
	std::string token;
	std::istringstream tokenStream(s);
	while (std::getline(tokenStream, token, delimiter)) {
		tokens.push_back(token);
	}
	return tokens;
}

Session::Session(Server& server, tcp::socket&& socket, int id)
	: server_(server)
	, ws_(std::move(socket))
	, id_(id)
	, lastReceivedTime_(std::chrono::system_clock::now()) {
}

bool Session::is_timed_out(std::chrono::system_clock::time_point now) const {
	auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastReceivedTime_).count();
	return elapsed > PLAYER_TIMEOUT_MS;
}

void Session::force_disconnect() {
	beast::error_code ec;
	ws_.next_layer().socket().close(ec);
}

int Session::get_id() const { return id_; }

bool Session::hasSpawnReserved() const { return hasReservedSpawn_; }
const Eigen::Vector3f& Session::reservedSpawn() const { return reservedSpawn_; }

bool Session::fetchStateAtTime(std::chrono::system_clock::time_point targetTime, ClientState& outState) const {
	if (timedClientStates.canFetchClientStateAtTime(targetTime)) {
		outState = timedClientStates.fetchClientStateAtTime(targetTime);
		return true;
	}
	return false;
}

void Session::send_message(const std::string& msg) {
	auto ss = std::make_shared<std::string>(msg);
	{
		std::lock_guard<std::mutex> lock(writeMutex_);
		writeQueue_.push(ss);
	}
	doWrite();
}

void Session::run() {
	{
		std::lock_guard<std::mutex> lock(server_.g_sessions_mutex);
		server_.g_sessions.push_back(shared_from_this());
	}

	ws_.async_accept([self = shared_from_this()](beast::error_code ec) {
		if (ec) return;
		std::cout << "Client " << self->id_ << " connected\n";
		self->init();
		});
}

bool Session::IsMessageValid(const std::string& fullMessage) {
#ifdef ENABLE_NETWORK_CHECKSUM
	size_t hashPos = fullMessage.find("#hash:");
	if (hashPos == std::string::npos) {
		return false; // Хеша нет, хотя он ожидался
	}

	std::string originalContent = fullMessage.substr(0, hashPos);
	std::string receivedHashStr = fullMessage.substr(hashPos + 6); // 6 — длина "#hash:"

	// Вычисляем ожидаемый хеш от контента
	size_t expectedHash = fnv1a_hash(originalContent + NET_SECRET);

	std::stringstream ss;
	ss << std::hex << expectedHash;

	return ss.str() == receivedHashStr;
#else
	return true; // В режиме отладки пропускаем всё
#endif
}

void Session::sendBoxesToClient() {
	std::lock_guard<std::mutex> lock(server_.g_boxes_mutex);

	std::string boxMsg = "BOXES:";
	bool first = true;

	for (size_t i = 0; i < server_.g_serverBoxes.size(); ++i) {
		const auto& box = server_.g_serverBoxes[i];

		if (box.destroyed) continue;

		Eigen::Quaternionf q(box.rotation);

		if (!first) boxMsg += "|";
		first = false;

		boxMsg += std::to_string(i) + ":" +
			std::to_string(box.position.x()) + ":" +
			std::to_string(box.position.y()) + ":" +
			std::to_string(box.position.z()) + ":" +
			std::to_string(q.w()) + ":" +
			std::to_string(q.x()) + ":" +
			std::to_string(q.y()) + ":" +
			std::to_string(q.z()) + ":" +
			"0";
	}

	send_message(boxMsg);
}


void Session::init()
{
	auto timer = std::make_shared<net::steady_timer>(ws_.get_executor());
	timer->expires_after(std::chrono::milliseconds(100));
	timer->async_wait([self = shared_from_this(), timer](const boost::system::error_code& ec) {
		if (!ec) {
			auto now_tp = std::chrono::system_clock::now();
			uint64_t now_ms = static_cast<uint64_t>(
				std::chrono::duration_cast<std::chrono::milliseconds>(now_tp.time_since_epoch()).count());

			self->sendBoxesToClient();

			self->send_message("ID:" + std::to_string(self->id_) + ":" + std::to_string(now_ms));
			self->do_read();
		}
		});
}

ClientState Session::get_latest_state(std::chrono::system_clock::time_point now) {
	if (timedClientStates.timedStates.empty()) {
		return {};
	}
	ClientState latest = timedClientStates.timedStates.back();
	latest.apply_lag_compensation(now);

	return latest;
}

void Session::doWrite() {
	std::lock_guard<std::mutex> lock(writeMutex_);
	if (is_writing_ || writeQueue_.empty()) {
		return;
	}

	is_writing_ = true;
	auto message = writeQueue_.front();

	ws_.async_write(net::buffer(*message),
		[self = shared_from_this(), message](beast::error_code ec, std::size_t) {
			if (ec) {
				std::cerr << "Write error: " << ec.message() << std::endl;
				return;
			}
			{
				std::lock_guard<std::mutex> lock(self->writeMutex_);
				self->writeQueue_.pop();
				self->is_writing_ = false;
			}
			self->doWrite();
		});
}

void Session::do_read() {
	ws_.async_read(buffer_, [self = shared_from_this()](beast::error_code ec, std::size_t) {
		if (ec) {
			if (self->joined_) {
				self->server_.broadcastToAllExceptId("PLAYER_LEFT:" + std::to_string(self->id_), self->id_);
				std::cout << "Client " << self->id_ << " disconnected, broadcasting PLAYER_LEFT\n";
			}
			std::lock_guard<std::mutex> lock(self->server_.g_sessions_mutex);
			self->server_.g_sessions.erase(std::remove_if(self->server_.g_sessions.begin(), self->server_.g_sessions.end(),
				[self](const std::shared_ptr<Session>& session) {
					return session.get() == self.get();
				}), self->server_.g_sessions.end());
			std::cout << "Client " << self->id_ << " removed from session list\n";
			return;
		}

		std::string msg = beast::buffers_to_string(self->buffer_.data());
		self->process_message(msg);

		self->buffer_.consume(self->buffer_.size());
		self->do_read();
		});
}

void Session::process_message(const std::string& msg) {
	if (!IsMessageValid(msg)) {
		std::cout << "[Security] Invalid packet hash. Dropping message: " << msg << std::endl;
		return;
	}
	lastReceivedTime_ = std::chrono::system_clock::now();
	std::string cleanMessage = msg.substr(0, msg.find("#hash:"));

	std::cout << "Received from player " << id_ << ": " << cleanMessage << std::endl;
	auto parts = split(cleanMessage, ':');

	if (parts.empty()) return;

	std::string type = parts[0];

	if (type == "JOIN") {
		std::string nick = "Player";
		int sType = 0;
		if (parts.size() >= 2) nick = parts[1];
		if (parts.size() >= 3) {
			try { sType = std::stoi(parts[2]); }
			catch (...) { sType = 0; }
		}

		this->nickname = nick;
		this->shipType = sType;
		this->joined_ = true;

		auto now_tp = std::chrono::system_clock::now();
		uint64_t now_ms = static_cast<uint64_t>(
			std::chrono::duration_cast<std::chrono::milliseconds>(now_tp.time_since_epoch()).count());

		Eigen::Vector3f spawnPos = server_.PickSafeSpawnPos(id_);
		this->hasReservedSpawn_ = true;
		this->reservedSpawn_ = spawnPos;

		ClientState st;
		st.id = id_;
		st.position = spawnPos;
		st.rotation = Eigen::Matrix3f::Identity();
		st.currentAngularVelocity = Eigen::Vector3f::Zero();
		st.velocity = 0.0f;
		st.selectedVelocity = 0;
		st.discreteMag = 0.0f;
		st.discreteAngle = -1;
		st.lastUpdateServerTime = now_tp;
		st.nickname = this->nickname;
		st.shipType = this->shipType;

		timedClientStates.add_state(st);

		this->send_message(
			"SPAWN:" + std::to_string(id_) + ":" + std::to_string(now_ms) + ":" + st.formPingMessageContent()
		);

		std::string eventMsg =
			"EVENT:" + std::to_string(id_) + ":UPD:" + std::to_string(now_ms) + ":" + st.formPingMessageContent();

		server_.broadcastToAllExceptId(eventMsg, id_);

		std::cout << "Server: Player " << id_ << " joined as [" << nick << "] shipType=" << sType << std::endl;

		std::string info = "PLAYERINFO:" + std::to_string(id_) + ":" + nick + ":" + std::to_string(sType);
		server_.broadcastToAllExceptId(info, id_);

		{
			std::lock_guard<std::mutex> lock(server_.g_sessions_mutex);
			for (auto& session : server_.g_sessions) {
				if (session->get_id() == this->id_) continue;
				if (!session->joined_) continue;
				std::string otherInfo = "PLAYERINFO:" + std::to_string(session->get_id()) + ":" + session->nickname + ":" + std::to_string(session->shipType);

				this->send_message(otherInfo);
			}
		}
	}
	else if (type == "UPD") {
		if (!joined_) {
			std::cout << "Server: Ignoring UPD before JOIN from " << id_ << std::endl;
			return;
		}
		{
			std::lock_guard<std::mutex> gd(server_.g_dead_mutex);
			if (server_.g_dead_players.find(id_) != server_.g_dead_players.end()) {
				std::cout << "Server: Ignoring UPD from dead player " << id_ << std::endl;
				return;
			}
		}

		if (parts.size() < 16) return;
		uint64_t clientTimestamp = std::stoull(parts[1]);
		ClientState receivedState;
		receivedState.id = id_;
		std::chrono::system_clock::time_point uptime_timepoint{
			std::chrono::milliseconds(clientTimestamp)
		};
		receivedState.lastUpdateServerTime = uptime_timepoint;
		receivedState.handle_full_sync(parts, 2);
		receivedState.nickname = this->nickname;
		receivedState.shipType = this->shipType;
		timedClientStates.add_state(receivedState);

	}
	else if (type == "RESPAWN") {
		{
			std::lock_guard<std::mutex> gd(server_.g_dead_mutex);
			server_.g_dead_players.erase(id_);
		}

		{
			auto now_tp = std::chrono::system_clock::now();
			uint64_t now_ms = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now_tp.time_since_epoch()).count());

			ClientState st;
			st.id = id_;

			Eigen::Vector3f spawnPos = server_.PickSafeSpawnPos(id_);
			st.position = spawnPos;

			this->hasReservedSpawn_ = true;
			this->reservedSpawn_ = spawnPos;

			st.rotation = Eigen::Matrix3f::Identity();
			st.currentAngularVelocity = Eigen::Vector3f::Zero();
			st.velocity = 0.0f;
			st.selectedVelocity = 0;
			st.discreteMag = 0.0f;
			st.discreteAngle = -1;
			st.lastUpdateServerTime = now_tp;
			st.nickname = this->nickname;
			st.shipType = this->shipType;

			timedClientStates.add_state(st);
			this->send_message(
				"SPAWN:" + std::to_string(id_) + ":" + std::to_string(now_ms) + ":" + st.formPingMessageContent()
			);
			std::string respawnMsg = "RESPAWN_ACK:" + std::to_string(id_);
			server_.broadcastToAll(respawnMsg);

			std::string playerInfo = "PLAYERINFO:" + std::to_string(id_) + ":" + st.nickname + ":" + std::to_string(st.shipType);
			server_.broadcastToAll(playerInfo);

			std::string eventMsg = "EVENT:" + std::to_string(id_) + ":UPD:" + std::to_string(now_ms) + ":" + st.formPingMessageContent();
			server_.broadcastToAll(eventMsg);

			std::cout << "Server: Player " << id_ << " respawned, broadcasted RESPAWN_ACK, PLAYERINFO and initial UPD\n";
		}
	}
	else if (parts[0] == "BOX_PICKUP") {
		if (parts.size() < 2) return;

		if (this->shipType != 1) {
			std::cout << "Server: Player " << id_ << " tried BOX_PICKUP but is not a cargo ship\n";
			return;
		}

		int boxIdx = -1;
		try { boxIdx = std::stoi(parts[1]); } catch (...) { return; }

		std::lock_guard<std::mutex> bm(server_.g_boxes_mutex);

		if (boxIdx < 0 || boxIdx >= (int)server_.g_serverBoxes.size()) return;
		if (server_.g_serverBoxes[boxIdx].destroyed) return;

		if (timedClientStates.timedStates.empty()) return;
		const ClientState& playerState = timedClientStates.timedStates.back();

		Eigen::Vector3f boxWorld = server_.g_serverBoxes[boxIdx].position + kWorldOffset;
		float distSq = (playerState.position - boxWorld).squaredNorm();
		if (distSq > BOX_PICKUP_RADIUS * BOX_PICKUP_RADIUS) {
			std::cout << "Server: Player " << id_ << " too far to pick up box " << boxIdx << "\n";
			return;
		}

		server_.g_serverBoxes[boxIdx].destroyed = true;

		std::string pickedUpMsg = "BOX_PICKED_UP:" + std::to_string(boxIdx) + ":" + std::to_string(id_);
		server_.broadcastToAll(pickedUpMsg);

		std::cout << "Server: Box " << boxIdx << " picked up by player " << id_ << "\n";

		// Respawn box
		{
			static thread_local std::mt19937 rng{ std::random_device{}() };
			static thread_local std::uniform_real_distribution<float> angleDist(0.f, static_cast<float>(M_PI * 2.0));
			Eigen::Vector3f newPos = server_.PickSafeBoxPos(boxIdx);
			Eigen::Vector3f axis = Eigen::Vector3f::Random().normalized();
			Eigen::Matrix3f newRot = Eigen::AngleAxisf(angleDist(rng), axis).toRotationMatrix();
			server_.g_serverBoxes[boxIdx].position = newPos;
			server_.g_serverBoxes[boxIdx].rotation = newRot;
			server_.g_serverBoxes[boxIdx].destroyed = false;

			Eigen::Quaternionf q(newRot);
			std::string respawnMsg = "BOX_RESPAWN:" +
				std::to_string(boxIdx) + ":" +
				std::to_string(newPos.x()) + ":" +
				std::to_string(newPos.y()) + ":" +
				std::to_string(newPos.z()) + ":" +
				std::to_string(q.w()) + ":" +
				std::to_string(q.x()) + ":" +
				std::to_string(q.y()) + ":" +
				std::to_string(q.z());
			server_.broadcastToAll(respawnMsg);
			std::cout << "Server: Box " << boxIdx << " respawned after pickup\n";
		}
	}
	else if (parts[0] == "FIRE") {
		if (parts.size() < 10) return;

		uint64_t clientTime = std::stoull(parts[1]);
		Eigen::Vector3f pos{
			std::stof(parts[2]), std::stof(parts[3]), std::stof(parts[4])
		};
		Eigen::Quaternionf dir(
			std::stof(parts[5]), std::stof(parts[6]), std::stof(parts[7]), std::stof(parts[8])
		);
		float velocity = std::stof(parts[9]);

		std::string broadcast = "PROJECTILE:" +
			std::to_string(id_) + ":" +
			std::to_string(clientTime) + ":" +
			std::to_string(pos.x()) + ":" +
			std::to_string(pos.y()) + ":" +
			std::to_string(pos.z()) + ":" +
			std::to_string(dir.w()) + ":" +
			std::to_string(dir.x()) + ":" +
			std::to_string(dir.y()) + ":" +
			std::to_string(dir.z()) + ":" +
			std::to_string(velocity);


		server_.broadcastToAllExceptId(broadcast, id_);

		server_.createProjectile(id_, pos, dir, velocity);
	}
}

Eigen::Vector3f Server::PickSafeBoxPos(int skipIdx)
{
	// Assumes g_boxes_mutex is already held by the caller
	static thread_local std::mt19937 rng{ std::random_device{}() };
	std::uniform_real_distribution<float> dist(-1000.f, 1000.f);

	for (int attempt = 0; attempt < 500; ++attempt) {
		Eigen::Vector3f cand(dist(rng), dist(rng), dist(rng));
		bool safe = true;
		for (int i = 0; i < (int)g_serverBoxes.size(); ++i) {
			if (i == skipIdx) continue;
			if (g_serverBoxes[i].destroyed) continue;
			if ((cand - g_serverBoxes[i].position).squaredNorm() < 9.f) {
				safe = false;
				break;
			}
		}
		if (safe) return cand;
	}
	return Eigen::Vector3f(dist(rng), dist(rng), dist(rng));
}

Eigen::Vector3f Server::PickSafeSpawnPos(int forPlayerId)
{
	static thread_local std::mt19937 rng{ std::random_device{}() };

	std::scoped_lock lock(g_boxes_mutex, g_sessions_mutex, g_dead_mutex);

	auto isSafe = [&](const Eigen::Vector3f& pWorld) -> bool
		{
			for (const auto& box : g_serverBoxes) {
				if (box.destroyed) continue;

				Eigen::Vector3f boxWorld = box.position + kWorldOffset;
				float minDist = kShipRadius + box.collisionRadius + kSpawnBoxMargin;

				if ((pWorld - boxWorld).squaredNorm() < minDist * minDist)
					return false;
			}

			for (const auto& s : g_sessions) {
				int pid = s->get_id();
				if (pid == forPlayerId) continue;
				if (g_dead_players.count(pid)) continue;

				Eigen::Vector3f otherPos;
				if (!s->timedClientStates.timedStates.empty()) {
					otherPos = s->timedClientStates.timedStates.back().position;
				}
				else if (s->hasSpawnReserved()) {
					otherPos = s->reservedSpawn();
				}
				else {
					continue;
				}

				float minDist = (kShipRadius * 2.0f) + kSpawnShipMargin;
				if ((pWorld - otherPos).squaredNorm() < minDist * minDist)
					return false;
			}

			return true;
		};

	const float radii[] = { 150.f, 250.f, 400.f, 650.f, 1000.f, 1600.f };

	for (float r : radii) {
		std::uniform_real_distribution<float> dxy(-r, r);
		std::uniform_real_distribution<float> dz(-kSpawnZJitter, kSpawnZJitter);

		for (int attempt = 0; attempt < 250; ++attempt) {
			Eigen::Vector3f cand(
				dxy(rng),
				dxy(rng),
				kWorldZOffset + dz(rng)
			);

			if (isSafe(cand))
				return cand;
		}
	}

	int a = (forPlayerId % 10);
	int b = ((forPlayerId / 10) % 10);
	return Eigen::Vector3f(600.0f + a * 100.0f, -600.0f + b * 100.0f, kWorldZOffset);
}

void Server::broadcastToAll(const std::string& message) {
	std::lock_guard<std::mutex> lock(g_sessions_mutex);
	for (const auto& session : g_sessions) {
		session->send_message(message);
	}
}

void Server::broadcastToAllExceptId(const std::string& message, int id)
{
	std::lock_guard<std::mutex> lock(g_sessions_mutex);
	for (auto& session : g_sessions) {
		if (session->get_id() == id) continue;
		session->send_message(message);
	}
}

void Server::createProjectile(int id, Eigen::Vector3f pos, Eigen::Quaternionf dir, float velocity)
{
	const std::vector<Eigen::Vector3f> localOffsets = {
		Eigen::Vector3f(-1.5f, 0.9f - 6.f, 5.0f),
		Eigen::Vector3f(1.5f, 0.9f - 6.f, 5.0f)
	};

	uint64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>((
		std::chrono::system_clock::now().time_since_epoch())).count();

	std::lock_guard<std::mutex> pl(g_projectiles_mutex);
	for (int i = 0; i < localOffsets.size(); ++i) {
		Projectile pr;
		pr.shooterId = id;
		pr.spawnMs = now_ms;
		Eigen::Vector3f shotPos = pos + dir.toRotationMatrix() * localOffsets[i];
		pr.pos = shotPos;
		Eigen::Vector3f localForward(0.0f, 0.0f, -1.0f);
		Eigen::Vector3f worldForward = dir.toRotationMatrix() * localForward;
		float len = worldForward.norm();
		if (len > 1e-6f) worldForward /= len;
		pr.vel = worldForward * velocity;
		pr.lifeMs = 15000.0f;
		g_projectiles.push_back(pr);

		std::cout << "Server: Created projectile from player " << id
			<< " at pos (" << shotPos.x() << ", " << shotPos.y() << ", " << shotPos.z()
			<< ") vel (" << pr.vel.x() << ", " << pr.vel.y() << ", " << pr.vel.z() << ")" << std::endl;
	}
}

void Server::update_world() {

	static auto last_snapshot_time = std::chrono::system_clock::now();

	auto now = std::chrono::system_clock::now();
	uint64_t now_ms = static_cast<uint64_t>(
		std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());

	// --- Detect and force-disconnect timed-out players ---
	{
		std::vector<std::shared_ptr<Session>> timedOut;
		{
			std::lock_guard<std::mutex> lock(g_sessions_mutex);
			for (auto& session : g_sessions) {
				if (session->is_timed_out(now)) {
					timedOut.push_back(session);
				}
			}
		}
		for (auto& session : timedOut) {
			std::cout << "Server: Player " << session->get_id()
				<< " timed out after " << PLAYER_TIMEOUT_MS << "ms, forcing disconnect\n";
			session->force_disconnect();
		}
	}

	{
		std::lock_guard<std::mutex> lock(g_sessions_mutex);
		for (auto& sender : g_sessions) {
			if (sender->timedClientStates.timedStates.empty()) continue;

			const ClientState& st = sender->timedClientStates.timedStates.back();
			uint64_t stateTime = static_cast<uint64_t>(
				std::chrono::duration_cast<std::chrono::milliseconds>(
					st.lastUpdateServerTime.time_since_epoch()).count());

			std::string event_msg = "EVENT:" + std::to_string(sender->get_id()) +
				":UPD:" + std::to_string(stateTime) + ":" + st.formPingMessageContent();

			for (auto& receiver : g_sessions) {
				if (receiver->get_id() != sender->get_id()) {
					receiver->send_message(event_msg);
				}
			}
		}
	}

	// --- Tick: projectile movement and hit detection ---
	const float dt = 50.0f / 1000.0f;
	std::vector<DeathInfo> deathEvents;

	{
		std::lock_guard<std::mutex> pl(g_projectiles_mutex);
		std::vector<int> indicesToRemove;

		for (size_t i = 0; i < g_projectiles.size(); ++i) {
			auto& pr = g_projectiles[i];

			pr.pos += pr.vel * dt;

			if (now_ms > pr.spawnMs + static_cast<uint64_t>(pr.lifeMs)) {
				indicesToRemove.push_back(static_cast<int>(i));
				continue;
			}

			bool hitDetected = false;

			{
				std::lock_guard<std::mutex> lm(g_sessions_mutex);
				std::lock_guard<std::mutex> gd(g_dead_mutex);

				for (auto& session : g_sessions) {
					int targetId = session->get_id();

					if (targetId == pr.shooterId) continue;
					if (g_dead_players.find(targetId) != g_dead_players.end()) continue;

					ClientState targetState;
					if (!session->fetchStateAtTime(now, targetState)) continue;

					Eigen::Vector3f diff = pr.pos - targetState.position;
					float combinedRadius = shipCollisionRadius + projectileHitRadius;

					if (diff.squaredNorm() <= combinedRadius * combinedRadius) {
						DeathInfo death;
						death.targetId = targetId;
						death.serverTime = now_ms;
						death.position = pr.pos;
						death.killerId = pr.shooterId;

						deathEvents.push_back(death);
						g_dead_players.insert(targetId);
						indicesToRemove.push_back(static_cast<int>(i));
						hitDetected = true;

						std::cout << "Server: *** HIT DETECTED! ***" << std::endl;
						std::cout << "Server: Projectile at ("
							<< pr.pos.x() << ", " << pr.pos.y() << ", " << pr.pos.z()
							<< ") hit player " << targetId << std::endl;
						break;
					}
				}
			}

			if (hitDetected) continue;
		}

		if (!indicesToRemove.empty()) {
			std::sort(indicesToRemove.rbegin(), indicesToRemove.rend());
			for (int idx : indicesToRemove) {
				if (idx >= 0 && idx < (int)g_projectiles.size()) {
					g_projectiles.erase(g_projectiles.begin() + idx);
				}
			}
		}
	}



	std::vector<int> boxesToRespawn;

	// --- Tick: box-projectile collisions ---
	{
		std::lock_guard<std::mutex> bm(g_boxes_mutex);


		std::vector<std::pair<size_t, size_t>> boxProjectileCollisions;

		for (size_t bi = 0; bi < g_serverBoxes.size(); ++bi) {
			if (g_serverBoxes[bi].destroyed) continue;

			Eigen::Vector3f boxWorld = g_serverBoxes[bi].position + Eigen::Vector3f(0.0f, 0.0f, 45000.0f);

			for (size_t pi = 0; pi < g_projectiles.size(); ++pi) {
				const auto& pr = g_projectiles[pi];
				Eigen::Vector3f diff = pr.pos - boxWorld;
				float thresh = boxCollisionRadius + projectileHitRadius;

				if (diff.squaredNorm() <= thresh * thresh) {
					boxProjectileCollisions.push_back({ bi, pi });
				}
			}
		}

		for (const auto& [boxIdx, projIdx] : boxProjectileCollisions) {
			if (g_serverBoxes[boxIdx].destroyed) continue;
			g_serverBoxes[boxIdx].destroyed = true;

			Eigen::Vector3f boxWorld = g_serverBoxes[boxIdx].position + Eigen::Vector3f(0.0f, 0.0f, 45000.0f);

			BoxDestroyedInfo destruction;
			destruction.boxIndex = static_cast<int>(boxIdx);
			destruction.serverTime = now_ms;
			destruction.position = boxWorld;
			destruction.destroyedBy = g_projectiles[projIdx].shooterId;

			{
				std::lock_guard<std::mutex> dm(g_boxDestructions_mutex);
				g_boxDestructions.push_back(destruction);
			}

			boxesToRespawn.push_back(static_cast<int>(boxIdx));

			std::cout << "Server: Box " << boxIdx << " destroyed by projectile from player "
				<< g_projectiles[projIdx].shooterId << std::endl;
		}
	}

	// --- Tick: box-ship collisions ---
	{
		std::lock_guard<std::mutex> bm(g_boxes_mutex);
		std::lock_guard<std::mutex> lm(g_sessions_mutex);

		for (size_t bi = 0; bi < g_serverBoxes.size(); ++bi) {
			if (g_serverBoxes[bi].destroyed) continue;

			Eigen::Vector3f boxWorld = g_serverBoxes[bi].position + Eigen::Vector3f(0.0f, 0.0f, 45000.0f);

			for (auto& session : g_sessions) {
				{
					std::lock_guard<std::mutex> gd(g_dead_mutex);
					if (g_dead_players.find(session->get_id()) != g_dead_players.end()) {
						continue;
					}
				}

				ClientState shipState;
				if (!session->fetchStateAtTime(now, shipState)) continue;

				Eigen::Vector3f diff = shipState.position - boxWorld;
				float thresh = shipCollisionRadius + boxCollisionRadius;

				if (diff.squaredNorm() <= thresh * thresh) {
					g_serverBoxes[bi].destroyed = true;

					BoxDestroyedInfo destruction;
					destruction.boxIndex = static_cast<int>(bi);
					destruction.serverTime = now_ms;
					destruction.position = boxWorld;
					destruction.destroyedBy = session->get_id();

					{
						std::lock_guard<std::mutex> dm(g_boxDestructions_mutex);
						g_boxDestructions.push_back(destruction);
					}

					boxesToRespawn.push_back(static_cast<int>(bi));

					std::cout << "Server: Box " << bi << " destroyed by ship collision with player "
						<< session->get_id() << std::endl;
					break;
				}
			}
		}
	}

	// --- Broadcast deaths ---
	for (const auto& death : deathEvents) {
		std::string deadMsg = "DEAD:" +
			std::to_string(death.serverTime) + ":" +
			std::to_string(death.targetId) + ":" +
			std::to_string(death.position.x()) + ":" +
			std::to_string(death.position.y()) + ":" +
			std::to_string(death.position.z()) + ":" +
			std::to_string(death.killerId);

		broadcastToAll(deadMsg);

		std::cout << "Server: Sent DEAD event - Player " << death.targetId
			<< " killed by " << death.killerId << std::endl;
	}

	// --- Broadcast box destructions ---
	{
		std::lock_guard<std::mutex> dm(g_boxDestructions_mutex);
		for (const auto& destruction : g_boxDestructions) {
			std::string boxMsg = "BOX_DESTROYED:" +
				std::to_string(destruction.boxIndex) + ":" +
				std::to_string(destruction.serverTime) + ":" +
				std::to_string(destruction.position.x()) + ":" +
				std::to_string(destruction.position.y()) + ":" +
				std::to_string(destruction.position.z()) + ":" +
				std::to_string(destruction.destroyedBy);

			broadcastToAll(boxMsg);
			std::cout << "Server: Broadcasted BOX_DESTROYED for box " << destruction.boxIndex << std::endl;
		}
		g_boxDestructions.clear();
	}

	// --- Respawn destroyed boxes ---
	if (!boxesToRespawn.empty()) {
		static thread_local std::mt19937 rng{ std::random_device{}() };
		static thread_local std::uniform_real_distribution<float> angleDist(0.f, static_cast<float>(M_PI * 2.0));
		std::vector<std::string> respawnMsgs;
		{
			std::lock_guard<std::mutex> bm(g_boxes_mutex);
			for (int idx : boxesToRespawn) {
				if (idx < 0 || idx >= (int)g_serverBoxes.size()) continue;
				Eigen::Vector3f newPos = PickSafeBoxPos(idx);
				Eigen::Vector3f axis = Eigen::Vector3f::Random().normalized();
				Eigen::Matrix3f newRot = Eigen::AngleAxisf(angleDist(rng), axis).toRotationMatrix();
				g_serverBoxes[idx].position = newPos;
				g_serverBoxes[idx].rotation = newRot;
				g_serverBoxes[idx].destroyed = false;

				Eigen::Quaternionf q(newRot);
				std::string respawnMsg = "BOX_RESPAWN:" +
					std::to_string(idx) + ":" +
					std::to_string(newPos.x()) + ":" +
					std::to_string(newPos.y()) + ":" +
					std::to_string(newPos.z()) + ":" +
					std::to_string(q.w()) + ":" +
					std::to_string(q.x()) + ":" +
					std::to_string(q.y()) + ":" +
					std::to_string(q.z());
				respawnMsgs.push_back(respawnMsg);
				std::cout << "Server: Box " << idx << " respawned" << std::endl;
			}
		}
		for (const auto& msg : respawnMsgs) {
			broadcastToAll(msg);
		}
	}

	// --- Schedule next tick in 50ms ---
	timer.expires_after(std::chrono::milliseconds(50));
	timer.async_wait([this](const boost::system::error_code& ec) {
		if (ec) return;
		update_world();
		});
}

std::vector<ServerBox> Server::generateServerBoxes(int count) {
	std::vector<ServerBox> boxes;
	std::random_device rd;
	std::mt19937 gen(rd());

	const float MIN_COORD = -1000.0f;
	const float MAX_COORD = 1000.0f;
	const float MIN_DISTANCE = 3.0f;
	const float MIN_DISTANCE_SQUARED = MIN_DISTANCE * MIN_DISTANCE;
	const int MAX_ATTEMPTS = 1000;

	std::uniform_real_distribution<> posDistrib(MIN_COORD, MAX_COORD);
	std::uniform_real_distribution<> angleDistrib(0.0, M_PI * 2.0);

	for (int i = 0; i < count; i++) {
		bool accepted = false;
		int attempts = 0;

		while (!accepted && attempts < MAX_ATTEMPTS) {
			ServerBox box;
			box.position = Eigen::Vector3f(
				(float)posDistrib(gen),
				(float)posDistrib(gen),
				(float)posDistrib(gen)
			);

			accepted = true;
			for (const auto& existingBox : boxes) {
				Eigen::Vector3f diff = box.position - existingBox.position;
				if (diff.squaredNorm() < MIN_DISTANCE_SQUARED) {
					accepted = false;
					break;
				}
			}

			if (accepted) {
				float randomAngle = (float)angleDistrib(gen);
				Eigen::Vector3f axis = Eigen::Vector3f::Random().normalized();
				box.rotation = Eigen::AngleAxisf(randomAngle, axis).toRotationMatrix();
				boxes.push_back(box);
			}

			attempts++;
		}
	}

	return boxes;
}

Server::Server(tcp::acceptor& acceptor, net::io_context& ioc)
	: acceptor_(acceptor)
	, ioc_(ioc)
	, timer(ioc_)
{
}

void Server::init()
{
	std::lock_guard<std::mutex> lock(g_boxes_mutex);
	g_serverBoxes = generateServerBoxes(50);
	std::cout << "Generated " << g_serverBoxes.size() << " boxes on server\n";
}

void Server::accept()
{
	acceptor_.async_accept([&](beast::error_code ec, tcp::socket socket) {
		if (!ec) {
			std::make_shared<Session>(*this, std::move(socket), next_id++)->run();
		}
		accept();
		});
}

int main() {
	try {
		net::io_context ioc;
		tcp::acceptor acceptor{ ioc, {tcp::v4(), 8081} };

		Server server(acceptor, ioc);

		server.init();
		server.accept();

		std::cout << "Server started on port 8081...\n";

		server.update_world();
		ioc.run();
	}
	catch (std::exception const& e) {
		std::cerr << "Error: " << e.what() << std::endl;
	}
	return 0;
}