using Domain.Entities;
using Domain.Generics;
using System.Net.Http.Json;
using Microsoft.JSInterop;

namespace phronCare.UIBlazor.Services.Stock
{
    public class ProductDivisionService
    {
        private readonly HttpClient _http;
        private readonly IJSRuntime _js;

        public ProductDivisionService(HttpClient http, IJSRuntime js)
        {
            _http = http;
            _js = js;
        }

        public async Task<List<ELSProductDivision>> GetAllAsync()
        {
            var result = await _http.GetFromJsonAsync<List<ELSProductDivision>>("/api/ProductDivision/GetAll");
            return result ?? new List<ELSProductDivision>();
        }

        public async Task<ELSProductDivision?> GetByIdAsync(int id)
        {
            var result = await _http.GetFromJsonAsync<ELSProductDivision>($"/api/ProductDivision/GetById/{id}");
            return result;
        }

        public async Task<HttpResponseMessage> CreateAsync(ELSProductDivision division)
        {
            return await _http.PostAsJsonAsync("/api/ProductDivision/Create", division);
        }

        public async Task<HttpResponseMessage> UpdateAsync(ELSProductDivision division)
        {
            return await _http.PutAsJsonAsync("/api/ProductDivision/Update", division);
        }

        public async Task<HttpResponseMessage> DeleteAsync(int id)
        {
            return await _http.DeleteAsync($"/api/ProductDivision/Delete/{id}");
        }

        public async Task<PagedResult<ELSProductDivision>?> SearchAsync(DivisionUnitSearchParams searchParams)
        {
            var url = $"/api/ProductDivision/Search?" +
                      $"term={searchParams.Term}&" +
                      $"page={searchParams.Page}&" +
                      $"pageSize={searchParams.PageSize}";
            return await _http.GetFromJsonAsync<PagedResult<ELSProductDivision>>(url);
        }
    }
}
