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

namespace phronCare.UIBlazor.Services.Stock
{
    public class LSUnitOfMeasureService
    {
        private readonly HttpClient _http;

        public LSUnitOfMeasureService(HttpClient http)
        {
            _http = http;
        }

        public async Task<PagedResult<ELSUnitOfMeasure>> SearchAsync(string? term, int page = 1, int pageSize = 50)
        {
            var response = await _http.GetFromJsonAsync<PagedResult<ELSUnitOfMeasure>>(
                $"api/LSUnitOfMeasure/Search?term={term}&page={page}&pageSize={pageSize}");
            return response!;
        }

        public async Task<ELSUnitOfMeasure?> GetByIdAsync(int id)
        {
            return await _http.GetFromJsonAsync<ELSUnitOfMeasure>($"api/LSUnitOfMeasure/GetById/{id}");
        }

        public async Task<int> CreateAsync(ELSUnitOfMeasure model)
        {
            var response = await _http.PostAsJsonAsync("api/LSUnitOfMeasure/Create", model);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadFromJsonAsync<int>();
        }

        public async Task UpdateAsync(ELSUnitOfMeasure model)
        {
            var response = await _http.PutAsJsonAsync("api/LSUnitOfMeasure/Update", model);
            response.EnsureSuccessStatusCode();
        }
    }
}
