using Azure.Core; using CPRNIMS.Domain.UIContracts.Common; using CPRNIMS.Domain.UIContracts.Inventory; using CPRNIMS.Infrastructure.Dto.Inventory.Reports; using CPRNIMS.Infrastructure.Dto.Inventory.Reports.Response; using CPRNIMS.Infrastructure.Dto.Inventory.Request; using CPRNIMS.Infrastructure.Helper; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Configuration; using System.Text; using System.Text.Json; namespace CPRNIMS.Domain.UIServices.Inventory { public class InventoryReports : IInventoryReports { private readonly IConfiguration _configuration; private readonly TokenHelper _tokenHelper; private readonly IApiConfigurationService _apiConfigurationService; public InventoryReports(IConfiguration configuration, TokenHelper tokenHelper, IApiConfigurationService apiConfigurationService) { _configuration = configuration; _tokenHelper = tokenHelper; _apiConfigurationService = apiConfigurationService; } private static readonly JsonSerializerOptions _jsonOpts = new() { PropertyNameCaseInsensitive = true }; public async Task GetInventoryReportAsync(InventoryReportsRequest request, CancellationToken ct) { var token = await _tokenHelper.GetValidTokenAsync(); if (string.IsNullOrEmpty(token)) throw new InvalidOperationException("Token has been expired."); var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:GetInventoryReport"] ?? throw new InvalidOperationException("GetInventoryReport endpoint is not configured."); var qs = new Dictionary { ["dateFrom"] = request.DateFrom.ToString("yyyy-MM-dd"), ["dateTo"] = request.DateTo.ToString("yyyy-MM-dd"), ["department"] = request.Department, ["page"] = request.Page.ToString(), ["pageSize"] = request.PageSize.ToString(), ["paginate"] = request.Paginate.ToString().ToLowerInvariant() }; var url = QueryHelpers.AddQueryString(baseEndpoint, qs); using var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync(url, ct); var json = await response.Content.ReadAsStringAsync(ct); if (!response.IsSuccessStatusCode) return new InventoryReportDto(); var result = JsonSerializer.Deserialize(json, _jsonOpts); return result ?? new InventoryReportDto(); } public async Task GetMRSReportAsync(InventoryReportsRequest request, CancellationToken ct) { var token = await _tokenHelper.GetValidTokenAsync(); if (string.IsNullOrEmpty(token)) throw new InvalidOperationException("Token has been expired."); var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:GetMRSReport"] ?? throw new InvalidOperationException("GetMRS endpoint is not configured."); var qs = new Dictionary { ["dateFrom"] = request.DateFrom.ToString("yyyy-MM-dd"), ["dateTo"] = request.DateTo.ToString("yyyy-MM-dd"), ["department"] = request.Department, ["page"] = request.Page.ToString(), ["pageSize"] = request.PageSize.ToString(), ["paginate"] = request.Paginate.ToString().ToLowerInvariant() }; var url = QueryHelpers.AddQueryString(baseEndpoint, qs); using var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync(url, ct); var json = await response.Content.ReadAsStringAsync(ct); if (!response.IsSuccessStatusCode) return new MRSReportDto { }; var result = JsonSerializer.Deserialize(json, _jsonOpts); return result ?? new MRSReportDto(); } public async Task GetRISReportAsync(InventoryReportsRequest request, CancellationToken ct) { var token = await _tokenHelper.GetValidTokenAsync(); if (string.IsNullOrEmpty(token)) throw new InvalidOperationException("Token has been expired."); var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:GetRISReport"] ?? throw new InvalidOperationException("GetMRS endpoint is not configured."); var qs = new Dictionary { ["dateFrom"] = request.DateFrom.ToString("yyyy-MM-dd"), ["dateTo"] = request.DateTo.ToString("yyyy-MM-dd"), ["department"] = request.Department, ["page"] = request.Page.ToString(), ["pageSize"] = request.PageSize.ToString(), ["paginate"] = request.Paginate.ToString().ToLowerInvariant() }; var url = QueryHelpers.AddQueryString(baseEndpoint, qs); using var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token); var response = await httpClient.GetAsync(url, ct); var json = await response.Content.ReadAsStringAsync(ct); if (!response.IsSuccessStatusCode) return new RISReportDto {}; var result = JsonSerializer.Deserialize(json, _jsonOpts); return result ?? new RISReportDto(); } } }