using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
using e_suite.Database.Audit;
using e_suite.Database.Core.Models;
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 EditSiteUnitTests : SiteManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

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

        var editSite = new EditSite
        {
            Id = new GeneralIdRef
            {
                Guid = new Guid("73d540dd-76d2-4d34-996a-88bcb3a2babe")
            }
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SiteManager.EditSite(auditUserDetails, editSite, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Site not found"));
    }

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

        var organisation = new Organisation
        {
            Guid = new Guid("e2b4ddd3-6746-4e13-ab2a-8f151357d92f"),
            Id = 2235412,
            Name = "My Organisation"
        };

        var site = new Site
        {
            Guid = new Guid("32622e33-a43f-4eef-82b6-fcb4beb76631"),
            Id = 2356,
            Name = "I R Gud speln",
            Address = "Near the place",
            Organisation = organisation,
            OrganisationId = organisation.Id,
            Status = OrganisationStatus.Active
        };
        SiteManagerRepository.Sites.Add(site);

        var editSite = new EditSite
        {
            Id = new GeneralIdRef
            {
                Guid = site.Guid
            },
            OrganisationId = new GeneralIdRef
            {
                Guid = new Guid("9109af41-a615-4a60-9615-a7c9e1a90547")
            }
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<InvalidOperationException>(async () =>
        {
            //Act
            await SiteManager.EditSite(auditUserDetails, editSite, true, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Not allow to move a site from one organisation to another"));
    }

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

        var organisation = new Organisation
        {
            Guid = new Guid("e2b4ddd3-6746-4e13-ab2a-8f151357d92f"),
            Id = 2235412,
            Name = "My Organisation"
        };

        var site = new Site
        {
            Guid = new Guid("32622e33-a43f-4eef-82b6-fcb4beb76631"),
            Id = 2356,
            Name = "I R Gud speln",
            Address = "Near the place",
            Organisation = organisation,
            OrganisationId = organisation.Id,
            Status = OrganisationStatus.Active
        };
        SiteManagerRepository.Sites.Add(site);

        var editSite = new EditSite
        {
            Id = new GeneralIdRef
            {
                Guid = site.Guid
            },
            OrganisationId = new GeneralIdRef
            {
                Guid = organisation.Guid
            },
            Name = "I am good at spelling",
            Address = "Second window on the left, next to the doorway",
            Status = OrganisationStatus.Pending
        };

        //Act
        await SiteManager.EditSite(auditUserDetails, editSite, true, CancellationToken.None);

        //Assert
        Assert.That( SiteManagerRepository.EditedSite, Is.Not.Null);
        Assert.That(SiteManagerRepository.EditedSite.Guid, Is.EqualTo(site.Guid));
        Assert.That(SiteManagerRepository.EditedSite.Id, Is.EqualTo(site.Id));
        Assert.That(SiteManagerRepository.EditedSite.Name, Is.EqualTo(editSite.Name));
        Assert.That(SiteManagerRepository.EditedSite.Address, Is.EqualTo(editSite.Address));
        Assert.That(SiteManagerRepository.EditedSite.Status, Is.EqualTo(editSite.Status));
        EFlowSyncMessageSenderMock.Verify( x => x.SyncEFlowPrinterCategories(), Times.Once);
    }
}