using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Ivrs;
using XF.Domain.Cache;
using XF.Domain.Dependency;

namespace Hotline.Caching.Services;

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 async Task<IReadOnlyList<Ivr>> GetIvrsAsync(CancellationToken cancellationToken)
    {
        var ivrs = await _ivrCache.GetOrSetAsync(Ivr.Key, d => _ivrRepository.QueryAsync().GetAwaiter().GetResult(), cancellationToken: cancellationToken);
        return ivrs ?? new List<Ivr>();
    }
}