namespace TaxBaik.Web.Services;

using System.Net.Http.Json;
using TaxBaik.Domain.Entities;

public interface ICategoryBrowserClient
{
    Task<IReadOnlyList<Category>> GetAllAsync(CancellationToken ct = default);
}

public class CategoryBrowserClient : ICategoryBrowserClient
{
    private readonly HttpClient _http;
    private readonly ILogger<CategoryBrowserClient> _logger;

    public CategoryBrowserClient(HttpClient http, ILogger<CategoryBrowserClient> logger)
    {
        _http = http;
        _logger = logger;
    }

    public async Task<IReadOnlyList<Category>> GetAllAsync(CancellationToken ct = default)
    {
        try
        {
            var result = await _http.GetFromJsonAsync<List<Category>>("category", cancellationToken: ct);
            return result ?? [];
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, "Failed to fetch categories");
            throw;
        }
    }
}
