using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.Context;
using PARR.DAL.Models.Unit;
using PARR.DAL.Services.Abstracts;
using PARR.DAL.Services.Interfaces.Unit;

namespace PARR.DAL.Services.Implementations.Unit
{
    internal class UnitFieldValueService : BaseService<UnitFieldValue>, IUnitFieldValueService
    {
        private readonly DataContext dataContext;

        protected override DbSet<UnitFieldValue> EntitySet => dataContext.UnitFieldValues;

        protected override DataContext EntitiContext => dataContext;

        public UnitFieldValueService(DataContext dataContext, ILogger<UnitFieldValueService> logger) : base(logger)
        {
            this.dataContext = 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());
        }
    }
}
