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

namespace Moonlight.App.MalwareScans;

public class FakePlayerPluginScan : MalwareScan
{
    public override string Name => "Fake player plugin scan";
    public override string Description => "This scan is a simple fake player plugin 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.IsFile && x.Name == "plugins")) // Check for plugins folder
        {
            await access.Cd("plugins");
            fileElements = await access.Ls();

            foreach (var fileElement in fileElements)
            {
                if (fileElement.Name.ToLower().Contains("fakeplayer"))
                {
                    return new()
                    {
                        Title = "Fake player plugin",
                        Description = $"Suspicious plugin file: {fileElement.Name}",
                        Author = "Marcel Baumgartner"
                    };
                }
            }
        }

        return null;
    }
}