using Pgo.Backend.Interfaces;
using Pgo.Backend.Models;

namespace Pgo.Backend.Clients;

public class StelselNodeApiClient(HttpClient httpClient) : IStelselNodeApiClient
{
    private readonly HttpClient _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));

    public async Task<StelselNodeApiResponse> GetZalAsync(string version, CancellationToken cancellationToken = default)
    {
        return await GetResponseApiAsync($"/MedMij_Zorgaanbiederslijst.xml?api={version}", cancellationToken);
    }

    private async Task<StelselNodeApiResponse> GetResponseApiAsync(string url, CancellationToken cancellationToken = default)
    {
        HttpResponseMessage httpResponse = await _httpClient.GetAsync(url, cancellationToken);
        var apiResponse = new StelselNodeApiResponse();

        if (httpResponse.IsSuccessStatusCode)
        {
            apiResponse.IsSuccessStatusCode = true;
            apiResponse.Content = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
        }
        return apiResponse;
    }
}