using e_suite.API.Common.exceptions;
using e_suite.Database.Audit;
using e_suite.Database.Core.Tables.Printer;
using e_suite.Modules.SpecificationManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using Moq;
using NUnit.Framework;

namespace e_suite.Modules.SpecificationManager.UnitTests;

[TestFixture]
public class DeleteSpecificationUnitTests : SpecificationManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void DeleteSpecification_WhenSpecificationNotFound_ThrowsNotFoundException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 243623,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("611d8925-81c5-4dd4-a2b3-b12508f3d983")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SpecificationManager.DeleteSpecification(auditUserDetails, generalIdRef, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Specification not found"));
    }

    [Test]
    public void DeleteSpecification_WhenSpecificationAlreadyDeleted_ThrowsNotFoundException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 243623,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var specification = new Specification()
        {
            Guid = new Guid("957b578f-278a-439d-a750-818b04a13504"),
            Id = 2134,
            Deleted = true
        };
        SpecificationManagerRepository.Specifications.Add(specification);

        var generalIdRef = new GeneralIdRef
        {
            Guid = specification.Guid,
        };
        
        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SpecificationManager.DeleteSpecification(auditUserDetails, generalIdRef, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Specification not found"));
    }

    [Test]
    public async Task DeleteSpecification_WhenSpecificationNotDeleted_DeletesSpeciciation()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 243623,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var specification = new Specification
        {
            Guid = new Guid("957b578f-278a-439d-a750-818b04a13504"),
            Id = 2134,
            Deleted = false
        };
        SpecificationManagerRepository.Specifications.Add(specification);

        var generalIdRef = new GeneralIdRef
        {
            Guid = specification.Guid,
        };

        //Act
        await SpecificationManager.DeleteSpecification(auditUserDetails, generalIdRef, true,CancellationToken.None);

        //Assert
        Assert.That(SpecificationManagerRepository.EditedSpecification, Is.Not.Null);
        Assert.That(SpecificationManagerRepository.EditedSpecification, Is.EqualTo(specification));
        Assert.That(SpecificationManagerRepository.EditedSpecification!.Deleted, Is.True);
        EFlowSyncMessageSenderMock.Verify(x => x.SyncEFlowPrinterCategories(), Times.Once);
    }
}