using Core.Interfaces.Stock;
using Domain.Dtos.Stock;
using Domain.Generics;
using Models.Interfaces;

namespace Core.Services.Stock
{
    public class LSStockScanService : ILSStockScanDom
    {
        #region Declaraciones y Constructor
        private readonly IPhLSMStockItemRepository _repo;
        // Constructor injection for repository dependency
        public LSStockScanService(IPhLSMStockItemRepository repo) => _repo = repo;
        #endregion
        #region Metodos
        public Task<PagedResult<StockItemScanResultDto>> SearchAsync(StockItemSearchParams p)
            => _repo.SearchStockItemsAsync(
                p.CodeOrText,
                p.Batch,
                p.LocationId,
                p.ProductType,
                p.TraceabilityType,
                p.PlusProcess,
                p.Page,
                p.PageSize);
        public Task<PagedResult<StockItemScanResultDto>> SearchParsedAsync(
            StockItemParsedSearchParams p,
            CancellationToken ct = default)
        {
            var page = p.Page <= 0 ? 1 : p.Page;
            var take = p.PageSize <= 0 ? 20 : p.PageSize;

            return _repo.SearchStockItemsParsedAsync(
                gtin: string.IsNullOrWhiteSpace(p.Gtin) ? null : p.Gtin!.Trim(),
                batch: string.IsNullOrWhiteSpace(p.Batch) ? null : p.Batch!.Trim(),
                expiration: p.Expiration,
                serial: string.IsNullOrWhiteSpace(p.Serial) ? null : p.Serial!.Trim(),
                locationId: p.LocationId,
                page: page,
                take: take
            );
        }
        #endregion
    }
}