// core/FilePath.h
#pragma once
#include <windows.h>
#include <string>

namespace FilePath
{
	/// @brief Gets the current executable's directory in UTF-8.
    inline std::string GetExecutableDirectory()
    {
        wchar_t wpath[MAX_PATH] = { 0 };
        if (!GetModuleFileNameW(nullptr, wpath, MAX_PATH))
            return {};

        std::wstring ws(wpath);
        size_t pos = ws.rfind(L'\\');
        if (pos == std::wstring::npos)
            return {};

        ws.resize(pos + 1);  // keep the \

        int size = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, nullptr, 0, nullptr, nullptr);
        if (size <= 1) return {};

        std::string result(size - 1, 0);
        WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), -1, result.data(), size, nullptr, nullptr);
        return result;
    }

    /// @brief Converts UTF-8 to std::wstring
    inline std::wstring Utf8ToWide(const std::string_view utf8)
    {
        if (utf8.empty()) return {};

        int n = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.length()), nullptr, 0);
        if (n == 0) return {};

        std::wstring wide(n, L'\0');
        MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.length()), wide.data(), n);
        return wide;
    }

    /// @brief Converts std::wstring to UTF-8
    inline std::string WideToUtf8(std::wstring_view wide)
    {
        if (wide.empty()) return {};

        int n = WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.length()), nullptr, 0, nullptr, nullptr);
        if (n == 0) return {};

        std::string utf8(n, 0);
        WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.length()), utf8.data(), n, nullptr, nullptr);
        return utf8;
    }
}