using Moonlight.App.Database.Entities;
using Moonlight.App.MalwareScans;
using Moonlight.App.Models.Misc;
using Moonlight.App.Services.Plugins;

namespace Moonlight.App.Services;

public class MalwareScanService
{
    private readonly PluginService PluginService;
    private readonly IServiceScopeFactory ServiceScopeFactory;

    public MalwareScanService(PluginService pluginService, IServiceScopeFactory serviceScopeFactory)
    {
        PluginService = pluginService;
        ServiceScopeFactory = serviceScopeFactory;
    }

    public async Task<MalwareScanResult?> Perform(Server server)
    {
        var defaultScans = new List<MalwareScan>
        {
            new MinerJarScan(),
            new FakePlayerPluginScan(),
            new BadScriptsScan()
        };

        var scans = await PluginService.BuildMalwareScans(defaultScans.ToArray());
        
        using var scope = ServiceScopeFactory.CreateScope();
        
        foreach (var scan in scans)
        {
            var result = await scan.Scan(server, scope.ServiceProvider);

            if (result != null)
                return result;
        }

        return null;
    }
}