using JSMR.Application.Integrations.DLSite.Models;
using JSMR.Application.Integrations.Ports;
using JSMR.Infrastructure.Http;
using JSMR.Infrastructure.Integrations.DLSite.Mapping;
using JSMR.Infrastructure.Integrations.DLSite.Models;
using Microsoft.Extensions.Logging;

namespace JSMR.Infrastructure.Integrations.DLSite;

public class DLSiteClient(IHttpService http, ILogger<DLSiteClient> logger) : ApiClient(http, logger), IDLSiteClient
{
    public async Task<VoiceWorkDetailCollection> GetVoiceWorkDetailsAsync(string[] productIds, CancellationToken cancellationToken = default)
    {
        if (productIds.Length == 0)
            return [];

        string productIdCollection = string.Join(",", productIds.Where(x => !string.IsNullOrWhiteSpace(x)));

        if (string.IsNullOrWhiteSpace(productIdCollection))
            return [];

        string url = $"maniax/product/info/ajax?product_id={productIdCollection}&cdn_cache_min=1";

        var productInfoCollection = await GetJsonAsync<ProductInfoCollection>(url, cancellationToken);

        return DLSiteToDomainMapper.Map(productInfoCollection);
    }
}