using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces.Unit;
using PARR.DAL.Context;
using PARR.DAL.Repositories.Base;
using PARR.Domain.Entities.Unit;

namespace PARR.DAL.Repositories.Unit
{
    internal class UnitFieldValueRepository : BaseRepository<UnitFieldValue>, IUnitFieldValueRepository
    {
        public UnitFieldValueRepository(DataContext dataContext, ILogger<UnitFieldValueRepository> logger) : base(logger, dataContext) { }


        public async Task<UnitFieldValue?> GetByValueNameAsync(string? value)
        {
            var query = EntitySet
                    .Include(v => v.FieldValues);

            if (string.IsNullOrWhiteSpace(value))
                return await query.FirstOrDefaultAsync(uf => uf.Value == null);

            return await query.FirstOrDefaultAsync(uf => uf.Value!.ToLower().Trim() == value.ToLower().Trim());
        }
    }
}
