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

namespace phronCare.UIBlazor.Services.Lookups
{
    public class SalesLookupService : ISalesLookupService
    {
        private readonly HttpClient _http;
        public SalesLookupService(HttpClient http) => _http = http;
        public Task<IEnumerable<ELookUpItem>> SearchCustomersAsync(string filtro)
            => FetchAsync($"api/lookup/customers?q={Uri.EscapeDataString(filtro)}", filtro);
        public Task<IEnumerable<ELookUpItem>> SearchProfessionalsAsync(string filtro)
            => FetchAsync($"api/lookup/professionals?q={Uri.EscapeDataString(filtro)}", filtro);
        public Task<IEnumerable<ELookUpItem>> SearchInstitutionsAsync(string filtro)
            => FetchAsync($"api/lookup/institutions?q={Uri.EscapeDataString(filtro)}", filtro);
        public Task<IEnumerable<ELookUpItem>> SearchPatientsAsync(string filtro)
            => FetchAsync($"api/lookup/patients?q={Uri.EscapeDataString(filtro)}", filtro);
        public Task<IEnumerable<ELookUpItem>> SearchPeopleAsync(string filtro)
            => FetchAsync($"api/lookup/people?q={Uri.EscapeDataString(filtro)}", filtro);
        public async Task<IEnumerable<ELookUpItem>> SearchBussinessUnitsAsync(string filtro)
        {
            var result = await _http.GetFromJsonAsync<List<EBusinessUnit>>("api/businessunit/all");

            return result?.Select(b => new ELookUpItem
            {
                Id = b.Id,
                Nombre = $"{b.Code} | {b.Description}"
            }) ?? Enumerable.Empty<ELookUpItem>();
        }
        public async Task<IEnumerable<ELookUpItem>> FetchAsync(string url,string filtro)
        {
            if (string.IsNullOrWhiteSpace(url) || filtro.Length < 3)
                return Array.Empty<ELookUpItem>();

            var items = await _http.GetFromJsonAsync<ELookUpItem[]>(url);
            return items ?? Array.Empty<ELookUpItem>();
        }
        public Task<IEnumerable<EProductLookupItem>> SearchProductsAsync(string filtro)
            => FetchProductsAsync($"api/lookup/products?q={Uri.EscapeDataString(filtro)}");
        public async Task<IEnumerable<EAdjustmentReason>> GetAdjustmentReasonsAsync()
        {
            var items = await _http.GetFromJsonAsync<EAdjustmentReason[]>("api/adjustmentreason/getall");
            return items ?? Array.Empty<EAdjustmentReason>();
        }
        private async Task<IEnumerable<EProductLookupItem>> FetchProductsAsync(string url)
        {
            var items = await _http.GetFromJsonAsync<EProductLookupItem[]>(url);
            return items ?? Array.Empty<EProductLookupItem>();
        }
        public async Task<IEnumerable<ETaxType>> GetTaxTypesAsync()
        {
            var items = await _http.GetFromJsonAsync<ETaxType[]>("api/taxtype/getall");
            return items ?? Array.Empty<ETaxType>();
        }
        public async Task<IEnumerable<ELookUpItem>> GetPaymentTermsAsync()
        {
            var items = await _http.GetFromJsonAsync<ELookUpItem[]>("api/lookup/paymentterms?q=");
            return items ?? Array.Empty<ELookUpItem>();
        }
        public Task<IEnumerable<ELookUpItem>> SearchApprovedQuotesAsync(string filtro)
            => FetchAsync($"api/lookup/approvedquotes?q={Uri.EscapeDataString(filtro)}", filtro);
    }
}
