using JSMR.Application.Common.Caching;
using JSMR.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace JSMR.Infrastructure.Caching;

public class SpamCircleCache(IDbContextFactory<AppDbContext> contextFactory, ICache cache)
    : CacheObject<string[]>(cache), ISpamCircleCache
{
    protected override string Key => "SpamCircles";

    protected override async Task<string[]> FetchAsync(CancellationToken cancellationToken = default)
    {
        using var context = contextFactory.CreateDbContext();

        return await context.Circles
            .AsNoTracking()
            .Where(circle => circle.Spam)
            .Select(circle => circle.MakerId)
            .Distinct()
            .ToArrayAsync(cancellationToken);
    }
}