using Hotline.Caches;
using Hotline.CallCenter.Ivrs;
using XF.Domain.Cache;
using XF.Domain.Dependency;

namespace Hotline.CacheManager;

public class IvrCacheManager : IIvrCacheManager, IScopeDependency
{
    private readonly ITypedCache<IReadOnlyList<Ivr>> _ivrCache;
    private readonly IIvrRepository _ivrRepository;

    public IvrCacheManager(ITypedCache<IReadOnlyList<Ivr>> ivrCache, IIvrRepository ivrRepository)
    {
        _ivrCache = ivrCache;
        _ivrRepository = ivrRepository;
    }

    public IReadOnlyList<Ivr> GetIvrs()
    {
        return _ivrCache.GetOrAdd(Ivr.Key, x => _ivrRepository.QueryAsync().GetAwaiter().GetResult());
    }
}