using UnityEngine;

/// <summary>
/// Initializes Sparkle auto-updater at app startup.
/// Uses RuntimeInitializeOnLoadMethod to ensure early initialization.
/// </summary>
public static class SparkleInitializer {
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    private static void Initialize() {
        Debug.Log(
                $"SparkleInitializer: App version {Application.version}, Unity {Application.unityVersion}");
        Debug.Log(
                $"SparkleInitializer: Platform: {Application.platform}, productName: {Application.productName}");
        Debug.Log("SparkleInitializer: Initializing Sparkle at startup");
        SparkleUpdater.Initialize();

#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
        Debug.Log(
                $"SparkleInitializer: AutomaticallyChecksForUpdates = {SparkleUpdater.AutomaticallyChecksForUpdates}");

        // Always check for updates on startup, regardless of scheduled check interval
        // This ensures users get updates promptly when a new version is available
        Debug.Log("SparkleInitializer: Checking for updates in background");
        SparkleUpdater.CheckForUpdatesInBackground();

        // Check if the app is running from a read-only volume (DMG)
        // If so, block launch and tell user to install properly
        if (SparkleUpdater.IsRunningFromReadOnlyVolume()) {
            Debug.LogError(
                    "SparkleInitializer: App is running from read-only volume (DMG), blocking launch");
            SparkleUpdater.ShowDMGWarning();
        }
#endif
    }
}
