#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

/// <summary>
/// 独立包启动时 Steamworks 只认 exe 同目录的 steam_appid.txt。
/// StreamingAssets 里的那份不会被 SteamAPI.Init 使用。
/// </summary>
public static class MJGameCopySteamAppId
{
    [PostProcessBuild(1000)]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        if (target != BuildTarget.StandaloneWindows && target != BuildTarget.StandaloneWindows64)
            return;

        var outputDir = Path.GetDirectoryName(pathToBuiltProject);
        if (string.IsNullOrEmpty(outputDir))
            return;

        var dest = Path.Combine(outputDir, "steam_appid.txt");
        var sources = new[]
        {
            Path.Combine(Directory.GetCurrentDirectory(), "steam_appid.txt"),
            Path.Combine(Application.dataPath, "StreamingAssets", "steam_appid.txt"),
            Path.Combine(Application.dataPath, "MJGameSDK", "Resources", "..", "..", "..", "steam_appid.txt"),
        };

        foreach (var source in sources)
        {
            var full = Path.GetFullPath(source);
            if (!File.Exists(full))
                continue;

            File.Copy(full, dest, true);
            Debug.Log($"[MJGameSDK] Copied steam_appid.txt -> {dest}");
            return;
        }

        File.WriteAllText(dest, "5052040\n");
        Debug.LogWarning($"[MJGameSDK] steam_appid.txt not found in project, wrote default AppId to {dest}");
    }
}
#endif
