using Backend.Data;
using Microsoft.EntityFrameworkCore;

namespace Backend.Services;

public sealed class RiskRuleService(AwardsDbContext db) : IRiskRuleService
{
    public async Task<RiskRuleSetting[]> GetRulesAsync(CancellationToken cancellationToken = default)
    {
        var settings = await db.SiteSettings
            .AsNoTracking()
            .FirstOrDefaultAsync(item => item.Id == 1, cancellationToken);

        return RiskRuleSettings.Read(settings);
    }

    public async Task<RiskRuleSetting> GetRuleAsync(string key, CancellationToken cancellationToken = default) =>
        RiskRuleSettings.Find(await GetRulesAsync(cancellationToken), key);
}
