using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace AsbCloudInfrastructure.Repository
{
#nullable enable
    public class CrudWellRelatedCacheServiceBase<TDto, TEntity> : CrudCacheServiceBase<TDto, TEntity>, ICrudWellRelatedService<TDto>
        where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated
        where TEntity : class, IId, IWellRelated
    {
        public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context)
            : base(context) { }

        public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
            : base(dbContext, includes) { }

        public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
            : base(context, makeQuery) { }

        public async Task<IEnumerable<TDto>> GetByIdWellAsync(int idWell, CancellationToken token)
        {
            var cache = await GetCacheAsync(token);

            var dtos = cache.Values
                .Where(e => e.IdWell == idWell)
                .ToList();

            return dtos;
        }

        public async Task<IEnumerable<TDto>> GetByIdWellAsync(IEnumerable<int> idsWells, CancellationToken token)
        {
            if (!idsWells.Any())
                return Enumerable.Empty<TDto>();

            var cache = await GetCacheAsync(token);

            var dtos = cache.Values
               .Where(e => idsWells.Contains(e.IdWell))
               .ToList();
            return dtos;
        }
    }
#nullable disable
}