using System.Text;
using System.Text.Json;
using e_suite.API.Common;
using e_suite.MessageProcessor.Handlers;
using e_suite.Messaging.Common;
using e_suite.Messaging.Common.models;
using e_suite.Service.Sentinel;
using e_suite.UnitTestCore;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using RabbitMQ.Client.Events;

namespace e_suite.MessageProcessor.UnitTests;

[TestFixture]
public class SigmaImportQueueHandlerUnitTests : TestBase
{
    protected Mock<ISentinelMaintenance> SentinelMaintenanceMock { get; set; } = null!;
    protected Mock<IPerformanceMaintenance> PerformanceMaintenanceMock { get; set; } = null!;
    protected Mock<IUserManagerMaintenance> UserManagerMaintenanceMock { get; set; } = null!;
    protected Mock<ISigmaImporter> SigmaImporterMock { get; set; } = null!;
    protected Mock<IEFlowSyncMessageSender> EFlowSyncMessageSenderMock { get; set; } = null!;
    protected Mock<ILogger> LoggerMock { get; set; } = null!;
    protected SigmaImportQueueHandler QueueHandler { get; set; } = null!;

    [SetUp]
    public override Task Setup()
    {
        SentinelMaintenanceMock = new Mock<ISentinelMaintenance>();
        PerformanceMaintenanceMock = new Mock<IPerformanceMaintenance>();
        UserManagerMaintenanceMock = new Mock<IUserManagerMaintenance>();
        SigmaImporterMock = new Mock<ISigmaImporter>();
        LoggerMock = new Mock<ILogger>();
        EFlowSyncMessageSenderMock = new Mock<IEFlowSyncMessageSender>();
        _fakeClock = new FakeClock();

        QueueHandler = new SigmaImportQueueHandler(_fakeClock, LoggerMock.Object, SigmaImporterMock.Object, EFlowSyncMessageSenderMock.Object);

        return base.Setup();
    }

    private static byte[] ObjectToStream<T>(T item)
    {
        var json = JsonSerializer.Serialize(item);
        return Encoding.UTF8.GetBytes(json);
    }

    [Test]
    public async Task OnReceived_ImportGMGProfiles_CallsImportGMGProfiles()
    {
        //Arrange
        var message = new SigmaImportMessage
        {
            MessageType = SigmaImportMessageTypes.ImportGMGProfiles
        };

        var body = ObjectToStream(message);

        var eventArgs = new BasicDeliverEventArgs(string.Empty, 0, false, string.Empty, string.Empty, null!, body);

        //Act
        await QueueHandler.OnReceived(MessageQueueNames.SigmaImport, eventArgs);

        //Assert
        SigmaImporterMock.Verify(x => x.ImportGMGProfiles(), Times.Once);
    }

    [Test]
    public async Task OnReceived_ImportPrintSpecifications_CallsImportPrintSpecifications()
    {
        //Arrange
        var message = new SigmaImportMessage
        {
            MessageType = SigmaImportMessageTypes.ImportPrintSpecifications
        };

        var body = ObjectToStream(message);

        var eventArgs = new BasicDeliverEventArgs(string.Empty, 0, false, string.Empty, string.Empty, null!, body);

        //Act
        await QueueHandler.OnReceived(MessageQueueNames.SigmaImport, eventArgs);

        //Assert
        SigmaImporterMock.Verify(x => x.ImportPrintSpecifications(), Times.Once);
        EFlowSyncMessageSenderMock.Verify( x => x.SyncEFlowPrinterCategories(), Times.Once);
    }
}