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

namespace Moonlight.App.MalwareScans;

public class MinerJarScan : MalwareScan
{
    public override string Name => "Miner jar scan";
    public override string Description => "This scan is a simple miner jar scan provided by moonlight";

    public override async Task<MalwareScanResult?> Scan(Server server, IServiceProvider serviceProvider)
    {
        var serverService = serviceProvider.GetRequiredService<ServerService>();
        var access = await serverService.CreateFileAccess(server, null!);
        var fileElements = await access.Ls();

        if (fileElements.Any(x => x.Name == "libraries" && !x.IsFile))
        {
            await access.Cd("libraries");
                
            fileElements = await access.Ls();

            if (fileElements.Any(x => x.Name == "jdk" && !x.IsFile))
            {
                return new()
                {
                    Title = "Found Miner",
                    Description = "Detected suspicious library directory which may contain a script for miners",
                    Author = "Marcel Baumgartner"
                };
            }
        }

        return null;
    }
}