#ifndef SPARKBUN_APP_PATHS_H
#define SPARKBUN_APP_PATHS_H

#include <string>

namespace sparkbun {

/**
 * Build the app data path using identifier/channel structure.
 * This ensures consistent path structure across all platforms and matches
 * the CLI and updater conventions.
 *
 * @param basePath The base application support/data path (e.g., ~/Library/Application Support)
 * @param identifier The app identifier (e.g., "sh.sparkbun-kitchen")
 * @param channel The release channel (e.g., "dev", "canary", "stable")
 * @param suffix Optional suffix to append (e.g., "CEF", "WebView2")
 * @param pathSeparator The path separator to use ('/' for Unix, '\\' for Windows)
 * @return The full path: basePath/identifier/channel/suffix
 */
inline std::string buildAppDataPath(
    const std::string& basePath,
    const std::string& identifier,
    const std::string& channel,
    const std::string& suffix = "",
    char pathSeparator = '/'
) {
    std::string appId = !identifier.empty() ? identifier : "SparkBun";
    std::string channelPath = !channel.empty() ? channel : "default";

    std::string result = basePath;
    result += pathSeparator;
    result += appId;
    result += pathSeparator;
    result += channelPath;

    if (!suffix.empty()) {
        result += pathSeparator;
        result += suffix;
    }

    return result;
}

/**
 * Build a partition-specific path under the app data directory.
 *
 * @param basePath The base application support/data path
 * @param identifier The app identifier
 * @param channel The release channel
 * @param renderer The renderer type (e.g., "CEF", "WebView2", "WebKit")
 * @param partitionName The partition name
 * @param pathSeparator The path separator to use
 * @return The full path: basePath/identifier/channel/renderer/Partitions/partitionName
 */
inline std::string buildPartitionPath(
    const std::string& basePath,
    const std::string& identifier,
    const std::string& channel,
    const std::string& renderer,
    const std::string& partitionName,
    char pathSeparator = '/'
) {
    std::string base = buildAppDataPath(basePath, identifier, channel, renderer, pathSeparator);
    base += pathSeparator;
    base += "Partitions";
    base += pathSeparator;
    base += partitionName;
    return base;
}

/**
 * Build a CEF partition-specific path under the renderer root.
 *
 * Partitions live in a `partitions/` subdirectory of the renderer cache root
 * rather than directly under it. The renderer root itself is CefSettings.cache_path,
 * which Chromium populates with auto-created profile directories such as
 * `Default`, `System Profile`, etc. On case-insensitive filesystems (Windows
 * NTFS, macOS APFS in default config) a partition literally named `default`
 * would collide with that auto-created `Default` folder; CEF then refuses to
 * bind a CefRequestContext to the colliding path and CreateBrowserSync
 * silently returns null. Nesting under `partitions/` keeps user partitions
 * cleanly separated from Chromium's own profile state.
 *
 * @param basePath The base application support/data path
 * @param identifier The app identifier
 * @param channel The release channel
 * @param renderer The renderer type (typically "CEF")
 * @param partitionName The partition name
 * @param pathSeparator The path separator to use
 * @return The full path: basePath/identifier/channel/renderer/partitions/partitionName
 */
inline std::string buildCEFPartitionPath(
    const std::string& basePath,
    const std::string& identifier,
    const std::string& channel,
    const std::string& renderer,
    const std::string& partitionName,
    char pathSeparator = '/'
) {
    std::string base = buildAppDataPath(basePath, identifier, channel, renderer, pathSeparator);
    base += pathSeparator;
    base += "partitions";
    base += pathSeparator;
    base += partitionName;
    return base;
}

} // namespace sparkbun

#endif // SPARKBUN_APP_PATHS_H
