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

namespace e_suite.Modules.SiteManager.UnitTests;

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

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

        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("60e7653e-be4c-4217-8ddb-cd2a78fdadc7")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SiteManager.DeleteSite(auditUserDetails, generalIdRef, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Site not found"));
    }
    
    [Test]
    public void DeleteSite_WhenSiteAlreadyDeleted_ThrowsException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 243623,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var site = new Site
        {
            Guid = new Guid("ebbe0f72-c93e-422a-806b-7c30d7f347dd"),
            Id = 235,
            Name = "Delete me",
            Deleted = true
        };
        SiteManagerRepository.Sites.Add(site);

        var generalIdRef = new GeneralIdRef
        {
            Guid = site.Guid
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<InvalidOperationException>(async () =>
        {
            //Act
            await SiteManager.DeleteSite(auditUserDetails, generalIdRef, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Site already deleted"));
    }

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

        var site = new Site
        {
            Guid = new Guid("ebbe0f72-c93e-422a-806b-7c30d7f347dd"),
            Id = 235,
            Name = "Delete me",
            Deleted = false
        };
        SiteManagerRepository.Sites.Add(site);

        var generalIdRef = new GeneralIdRef
        {
            Guid = site.Guid
        };

        //Act
        await SiteManager.DeleteSite(auditUserDetails, generalIdRef, true, CancellationToken.None);

        //Assert
        var modifiedSite = SiteManagerRepository.Sites[0];
        Assert.That(modifiedSite.Deleted, Is.True);
        EFlowSyncMessageSenderMock.Verify(x => x.SyncEFlowPrinterCategories(), Times.Once);
    }
}