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

namespace PARR.DAL.Repositories.Unit
{
    internal class UnitInUnitRepository : IUnitInUnitRepository
    {
        private readonly DataContext dataContext;
        private readonly ILogger<UnitInUnitRepository> logger;

        public UnitInUnitRepository(
            DataContext dataContext,
            ILogger<UnitInUnitRepository> logger
            )
        {
            this.dataContext = dataContext;
            this.logger = logger;
        }


        public IQueryable<UnitInUnit> Get()
        {
            return dataContext.UnitInUnits;
        }


        public Task<List<UnitInUnit>> GetByParentIdAsync(Guid parentId)
        {
            return dataContext.UnitInUnits
                .Where(u => u.ParentUnitId == parentId)
                .ToListAsync();
        }


        public Task<List<UnitInUnit>> GetByChildIdAsync(Guid childId)
        {
            return dataContext.UnitInUnits
                .Where(u => u.ChildUnitId == childId)
                .ToListAsync();
        }


        public async Task<List<UnitInUnit>> GetParentLinksByChildIdsAsync(IEnumerable<Guid> childUnitIds)
        {
            var set = childUnitIds.ToHashSet();
            return await dataContext.UnitInUnits
                .AsNoTracking()
                .Where(uinu => set.Contains(uinu.ChildUnitId))
                .ToListAsync();
        }

        public async Task<List<UnitInUnit>> GetChildLinksByParentIdsAsync(IEnumerable<Guid> parentUnitIds)
        {
            var set = parentUnitIds.ToHashSet();
            return await dataContext.UnitInUnits
                .AsNoTracking()
                .Where(uinu => set.Contains(uinu.ParentUnitId))
                .ToListAsync();
        }
    }
}
