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 UnitFieldRepository : BaseRepository<UnitField>, IUnitFieldRepository
    {
        public UnitFieldRepository(DataContext dataContext, ILogger<UnitFieldRepository> logger) : base(logger, dataContext) { }
        public async Task<UnitField?> GetByCodeAsync(string code, CancellationToken ct = default)
        {
            if (string.IsNullOrWhiteSpace(code))
                return null;

            return await Get()
                .AsNoTracking()
                .FirstOrDefaultAsync(f => f.Code != null && EF.Functions.ILike(f.Code, code), ct);
        }

        public async Task<UnitField?> GetByAihitNameAsync(string aihitName, CancellationToken ct = default)
        {
            if (string.IsNullOrWhiteSpace(aihitName))
                return null;

            return await Get()
                .AsNoTracking()
                .FirstOrDefaultAsync(f => EF.Functions.ILike(f.AihitName, aihitName), ct);
        }
    }
}
