using System.Net.Http.Json;
using MicCheck.Common;

namespace MicCheck;

public class FeatureClient(HttpClient httpClient) : IFeatureClient
{
    public async Task<bool> IsEnabledAsync(string featureName, bool defaultValue = false, CancellationToken cancellationToken = default)
    {
        Guard.Empty(featureName, nameof(featureName));

        var flags = await httpClient.GetFromJsonAsync<List<FlagResult>>("api/v1/flags", cancellationToken);
        var match = flags?.FirstOrDefault(flag => string.Equals(flag.Feature.Name, featureName, StringComparison.OrdinalIgnoreCase));

        return match?.Enabled ?? defaultValue;
    }
}

public interface IFeatureClient
{
    Task<bool> IsEnabledAsync(string featureName, bool defaultValue = false, CancellationToken cancellationToken = default);
}
