#include "utilities/Logger.h"
#include <iostream>

void Log::Message(Level Level, const std::string& Message, const std::source_location& Location)
{
	std::string FullFunc = Location.function_name();
	std::string DisplayName = FullFunc; // Default to full function name

	// Simple cleanup: try to extract just class::method
	size_t LastColon = FullFunc.rfind("::");
	if (LastColon != std::string::npos)
	{
		// Find the class name start
		size_t ClassStart = FullFunc.rfind(' ', LastColon);
		if (ClassStart != std::string::npos)
		{
			std::string ClassName = FullFunc.substr(ClassStart + 1, LastColon - ClassStart - 1);

			// Find method name end (first parenthesis after last ::)
			size_t MethodEnd = FullFunc.find('(', LastColon);
			if (MethodEnd != std::string::npos)
			{
				std::string MethodName = FullFunc.substr(LastColon + 2, MethodEnd - LastColon - 2);
				DisplayName = ClassName + "::" + MethodName + "()";
			}
		}
	}

	std::string LevelString;
	switch (Level)
	{
		case Level::Info:
			LevelString = "INFO";
			break;
		case Level::Warning:
			LevelString = "WARNING";
			break;
		case Level::Error:
			LevelString = "ERROR";
			break;
		case Level::Debug:
			LevelString = "DEBUG";
			break;
		case Level::VkValidation:
			LevelString = "VALIDATION LAYER";
			break;
		case Level::DeviceSetup:
			LevelString = "DEVICE SETUP";
			break;
		default:
			LevelString = "UNKNOWN";
			break;
	}

	std::string LogMessage = std::format("[{}] [{}] {}",
		LevelString, DisplayName, Message);

	std::cout << LogMessage << std::endl;

	if (Level == Level::Error)
	{
		throw std::runtime_error(LogMessage);
	}
}
