using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.SystemAlerts;
using adas_core.Infrastructure.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;

namespace adas_core.Test.Services;

[TestFixture]
public class SendAlertServiceTest
{
    /// <summary>
    /// Initializes test dependencies for <see cref="SendAlertService"/> unit tests, including
    /// RabbitMQ options, a mocked logger, an instance of the service under test, and the set
    /// of Windows platform identifiers used to validate platform-specific behavior.
    /// </summary>
    [SetUp]
        public void Setup()
        {
            _optionsRabbitMqSettings = Options.Create(_rabbitMqSettings);
    
            _logger = new Mock<ILogger<SendAlertService>>();
    
            _sendAlertService = new SendAlertService(
                _optionsRabbitMqSettings,
                _logger.Object
            );
    
            _windowsPlatforms =
            [
                PlatformID.Win32NT,
                PlatformID.Win32S,
                PlatformID.Win32Windows,
                PlatformID.WinCE
            ];
        }

    private SendAlertService _sendAlertService = null!;

    private readonly RabbitMqSettings _rabbitMqSettings = new();
    private IOptions<RabbitMqSettings> _optionsRabbitMqSettings = null!;

    private Mock<ILogger<SendAlertService>> _logger = null!;

    private PlatformID[] _windowsPlatforms = null!;

    /// <summary>
    /// Verifies that <see cref="_sendAlertService"/>.<c>GetConsumedCpu</c> returns a valid performance CPU
    /// reading (non-null with a positive <c>ValueTotal</c>) when executed on a Windows platform. When the
    /// host operating system is not Windows, the test is skipped with an ignore notice.
    /// </summary>
    [Ignore("Integration test to pull request")]
        [Test]
        public void GetConsumedCpu_Return_PerformanceCpu_data()
        {
            if (_windowsPlatforms.Contains(Environment.OSVersion.Platform))
            {
                var result = _sendAlertService.GetConsumedCpu();
                //result = await sendAlertService.GetConsumedCpu();
    
                Assert.That(result, Is.Not.Null);
                Assert.That(result.ValueTotal, Is.GreaterThan(0));
            }
            else
            {
                Assert.Ignore("This test can only be run on Windows.");
            }
        }


    /// <summary>
    /// Verifies that <c>GetConsumedRam</c> returns a non-null performance RAM data object with positive values
    /// for <c>PercentageConsumed</c>, <c>ValueConsumed</c>, and <c>ValueTotal</c>. The test only executes on
    /// Windows platforms and is ignored on other operating systems.
    /// </summary>
    [Ignore("Integration test to pull request")]
        [Test]
        public void GetConsumedRAM_Return_PerformanceRAM_data()
        {
            if (_windowsPlatforms.Contains(Environment.OSVersion.Platform))
            {
                var result = _sendAlertService.GetConsumedRam();
                //result = await sendAlertService.GetConsumedRAM();
    
                Assert.That(result, Is.Not.Null);
                using (Assert.EnterMultipleScope())
                {
                    Assert.That(result.PercentageConsumed, Is.GreaterThan(0));
                    Assert.That(result.ValueConsumed, Is.GreaterThan(0));
                    Assert.That(result.ValueTotal, Is.GreaterThan(0));
                }
            }
            else
            {
                Assert.Ignore("This test can only be run on Windows.");
            }
        }

    /// <summary>
    /// Integration test that verifies the GetConsumedStorage method returns valid Performance data with positive percentage and value metrics for all ready drives on Windows platforms. The test is ignored when executed on a non-Windows platform.
    /// </summary>
    [Ignore("Integration test to pull request")]
        [Test]
        public void GetConsumedStorage_Return_PerformanceStorage_data()
        {
            if (_windowsPlatforms.Contains(Environment.OSVersion.Platform))
            {
                List<Performance> performanceList = [];
    
                foreach (var drive in DriveInfo.GetDrives())
                    if (drive.IsReady)
                    {
                        var performance = _sendAlertService.GetConsumedStorage(drive);
    
                        performanceList.Add(performance);
                    }
    
                Assert.That(performanceList, Is.Not.Null);
                using (Assert.EnterMultipleScope()) 
                {
                    Assert.That(performanceList, Is.Not.Empty);
                    Assert.That(performanceList[0].PercentageConsumed, Is.GreaterThan(0));
                    Assert.That(performanceList[0].ValueConsumed, Is.GreaterThan(0));
                    Assert.That(performanceList[0].ValueTotal, Is.GreaterThan(0));
                }
            }
            else
            {
                Assert.Ignore("This test can only be run on Windows.");
            }
        }
}