using NerdRank;
using UnityEngine;

public class NerdRankLeaderboardExample : MonoBehaviour
{
    [SerializeField] private NerdRankSettings settings;
    [SerializeField] private string leaderboardId;
    [SerializeField] private string playerName = "Player";
    [SerializeField] private string userId;
    [SerializeField] private int entriesToRetrieve = 10;

    private NerdRankClient client;

    private void Awake()
    {
        client = new NerdRankClient(settings);

        if (string.IsNullOrWhiteSpace(userId))
        {
            userId = System.Guid.NewGuid().ToString();
        }
    }

    public void SubmitScore(float score)
    {
        StartCoroutine(client.AddLeaderboardEntry(
            leaderboardId,
            playerName,
            score,
            userId,
            result =>
            {
                if (result.Success)
                {
                    Debug.Log($"NerdRank score submitted: {result.Value.id}");
                }
                else
                {
                    Debug.LogError($"NerdRank submit failed: {result.Error}");
                }
            }));
    }

    public void LoadLeaderboard()
    {
        StartCoroutine(client.GetLeaderboard(
            leaderboardId,
            entriesToRetrieve,
            result =>
            {
                if (!result.Success)
                {
                    Debug.LogError($"NerdRank leaderboard load failed: {result.Error}");
                    return;
                }

                var entries = result.Value.entries;
                Debug.Log($"NerdRank loaded {entries?.Length ?? 0} entries for {result.Value.name}");
            }));
    }
}
