using UnityEngine;
using System.IO;

public class SaveManager : MonoBehaviour
{
    private string filePath;

    private void Start()
    {
        filePath = Application.persistentDataPath + "/savegame.json";
    }

    public void SaveGame(GameData data)
    {
        if (filePath == null) 
            filePath = Application.persistentDataPath + "/savegame.json";

        string json = JsonUtility.ToJson(data);
        File.WriteAllText(filePath, json);
        Debug.Log("Game Saved to " + filePath);
    }

    public GameData LoadGame()
    {
        if (filePath == null)
            filePath = Application.persistentDataPath + "/savegame.json";

        if (File.Exists(filePath))
        {
            string json = File.ReadAllText(filePath);
            GameData data = JsonUtility.FromJson<GameData>(json);
            Debug.Log("Game loaded from " + filePath);
            return data;
        }
        else
        {
            Debug.Log("Save file not found!");
            return null;
        }
    }
}
