using Domain.Entities;
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces;
using Models.Models;

namespace Models.Repositories.Stock
{
    public class PhLSMUnitOfMeasureRepository(PhronCareOperationsHubContext context) : IPhLSMUnitOfMeasureRepository
    {
        private readonly PhronCareOperationsHubContext _context = context;
        public async Task<PagedResult<ELSUnitOfMeasure>> GetAllAsync(int page = 1, int pageSize = 100)
        {
            var query = _context.PhLsmUnitOfMeasures.AsQueryable();
            var paged = await query.ToPagedResultAsync(page, pageSize);

            return new PagedResult<ELSUnitOfMeasure>
            {
                Items = paged.Items.Select(EntityMapper.MapEntity<PhLsmUnitOfMeasure, ELSUnitOfMeasure>),
                TotalItems = paged.TotalItems,
                Page = paged.Page,
                PageSize = paged.PageSize
            };
        }
        public async Task<bool> ExistsByCodeAsync(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
                return false;

            return await _context.PhLsmUnitOfMeasures
                .AnyAsync(x => x.Code.ToLower() == code.ToLower());
        }
        public async Task<List<string>> GetAllCodesAsync()
        {
            return await _context.PhLsmUnitOfMeasures
                .Select(x => x.Code)
                .ToListAsync();
        }
    }
}
