using Microsoft.Extensions.Logging;
using PARR.Core.Services.UnitService.Interfaces;
using PARR.Domain.DTOs.UnitDto;

namespace PARR.Core.Services.UnitService.Implementations
{
    internal class UnitService : IUnitService
    {
        private readonly ILogger<UnitService> logger;
        private readonly UnitCacheService unitCacheService;

        public UnitService(
                ILogger<UnitService> logger,
                UnitCacheService unitCacheService
            )
        {
            this.logger = logger;
            this.unitCacheService = unitCacheService;
        }


        public async Task<UnitInfo?> GetWithCachingAsync(Guid id)
        {
            return await unitCacheService.GetAsync(id);
        }

        public async Task<Dictionary<Guid, UnitInfo>> GetWithCachingAsync(IEnumerable<Guid> ids)
        {
            // Защищаем себя от Multiple Enumeration и убираем дубликаты, если передали List
            var uniqueIds = ids as IReadOnlySet<Guid> ?? ids.ToHashSet();

            return await unitCacheService.GetAsync(uniqueIds);
        }

        public async Task RemoveFromCacheAsync(Guid id)
        {
            await unitCacheService.RemoveFromCacheAsync(id);
        }

        public async Task RemoveFromCacheAsync(IEnumerable<Guid> ids)
        {
            // Защищаем себя от Multiple Enumeration и убираем дубликаты, если передали List
            var uniqueIds = ids as IReadOnlySet<Guid> ?? ids.ToHashSet();

            await unitCacheService.RemoveFromCacheAsync(uniqueIds);
        }
    }
}
