using e_suite.API.Common.models;
using e_suite.Database.Core.Models;
using e_suite.Modules.OrganisationManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using Moq;
using NUnit.Framework;

namespace e_suite.Modules.OrganisationManager.UnitTests;

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

    [Test]
    public async Task AddOrganisation_WhenGuidAlreadyExists_ThrowsArgumentException()
    {
        // Arrange
        var organisation1 = new CreateOrganisation
        {
            Guid = new Guid("f215fefc-369a-4d83-a8eb-8de7cf3e4539"),
            Name = "Sun Strategy",
            Address = "London, United Kingdom",
            Status = OrganisationStatus.Active
        };

        await OrganisationManager.AddOrganisation(AuditUserDetails, organisation1, true, default);

        var organisation2 = new CreateOrganisation
        {
            Guid = organisation1.Guid,
            Name = "Microsoft",
            Address = "Mountain View, USA",
            Status = OrganisationStatus.Active
        };

        
        // Assert
        Assert.ThrowsAsync<ArgumentException>(async () =>
        {
            // Act
            await OrganisationManager.AddOrganisation(AuditUserDetails, organisation2, true, default);
        });
    }

    [Test]
    public async Task AddOrganisation_WhenOrganisationNameAlreadyExists_ThrowsArgumentException()
    {
        // Arrange
        var organisation1 = new CreateOrganisation
        {
            Guid = new Guid("34c3320d-0625-4f93-ad21-b98ada6b1270"),
            Name = "Sun Strategy",
            Address = "London, United Kingdom",
            Status = OrganisationStatus.Active
        };

        await OrganisationManager.AddOrganisation(AuditUserDetails, organisation1, true, default);

        var organisation2 = new CreateOrganisation
        {
            Guid = new Guid("60b46146-bd8a-4c0e-ad0f-eb939970b394"),
            Name = organisation1.Name,
            Address = "Mountain View, USA",
            Status = OrganisationStatus.Active
        };
        
        // Assert
        Assert.ThrowsAsync<ArgumentException>(async () =>
        {
            // Act
            await OrganisationManager.AddOrganisation(AuditUserDetails, organisation2, true, default);
        });
    }

    [Test]
    public async Task AddOrganisation_WhenNotDuplicates_CreatesOrfanisation()
    {
        // Arrange
        var organisation = new CreateOrganisation
        {
            Guid = new Guid("0f493204-052a-4b66-ac5d-e4621edddd9a"),
            Name = "Sun Strategy",
            Address = "London, United Kingdom",
            Status = OrganisationStatus.Active
        };

        // Act
        await OrganisationManager.AddOrganisation(AuditUserDetails, organisation, true, default);

        // Assert
        var result = await OrganisationManager.GetOrganisation(new GeneralIdRef { Guid = organisation.Guid }, default);
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Name, Is.EqualTo(organisation.Name));
        EFlowSyncMessageSenderMock.Verify(x => x.SyncEFlowPrinterCategories(), Times.Once);
    }
}