using System.Text.Json; using GB5Shared.DTO.Framework.Login; using GB5Shared.ScanIdentifier; using WiDAL.CustomCode.WiMaster; namespace WiBLL.ScanTarget; // Phase 1 resolver for the Scan-Identifier & Label Platform (GB5Shared/ScanIdentifier) — "MWI". public class WiEntityResolver : IScanTargetResolver { private readonly IWiMasterDAL _dal; public WiEntityResolver(IWiMasterDAL dal) => _dal = dal; public IEnumerable SupportedEntityTypes => new[] { "MWI" }; public async Task ExistsAsync(string entityId, LoginDTO login, CancellationToken ct) => int.TryParse(entityId, out var wiId) && await _dal.GetWiById(wiId, login, ct).ConfigureAwait(false) is not null; public async Task GetDisplayAsync(string entityId, LoginDTO login, CancellationToken ct) { if (!int.TryParse(entityId, out var wiId)) return null; var wi = await _dal.GetWiById(wiId, login, ct).ConfigureAwait(false); return wi is null ? null : new ScanTargetDisplayDTO { EntityId = entityId, Code = wi.WiCode, Name = wi.WiTitle }; } // GetWiList has no text-search parameter — filters the full active-status list in memory. // Adequate for a picker; revisit with a real search query if WI counts grow large enough to matter. public async Task> SearchAsync(string? term, LoginDTO login, CancellationToken ct) { var json = await _dal.GetWiList(-1, 255, login, ct).ConfigureAwait(false); var rows = JsonSerializer.Deserialize>(json, JsonOpts) ?? []; var filtered = string.IsNullOrWhiteSpace(term) ? rows : rows.Where(r => r.WiCode.Contains(term, StringComparison.OrdinalIgnoreCase) || r.WiTitle.Contains(term, StringComparison.OrdinalIgnoreCase)); return filtered.Take(20).Select(r => new ScanTargetDisplayDTO { EntityId = r.WiId.ToString(), Code = r.WiCode, Name = r.WiTitle }); } private static readonly JsonSerializerOptions JsonOpts = new(JsonSerializerDefaults.Web); private sealed class WiListRow { public int WiId { get; set; } public string WiCode { get; set; } = string.Empty; public string WiTitle { get; set; } = string.Empty; } }