using e_suite.API.Common.exceptions;
using eSuite.Core.MailService;
using eSuite.Core.Miscellaneous;
using Moq;
using NUnit.Framework;
using UserManager.UnitTests.Helpers;

namespace UserManager.UnitTests.UserManager;

[TestFixture]
public class ResendConfirmEmailUnitTests : UserManagerTestBase<object>
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void ResendConfirmEmail_UserNotFound_ThrowsNotFoundException()
    {
        //Arrange
        var user = new GeneralIdRef
        {
            Guid = new Guid("f9977d27-423f-4d80-9c20-b379f5d08af4")
        };

        //Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await UserManager.ResendConfirmEmail(AuditUserDetails, user, CancellationToken.None);
        });
    }

    [Test]
    public async Task ResendConfirmEmail_UserExists_SendsConfirmationEmail()
    {
        //Arrange
        var user = new GeneralIdRef
        {
            Guid = new Guid("{EA6A2EB2-ADBF-47A1-8FA6-EF363D0750DC}")
        };

        MailRequest actualMailRequest = null!;

        MailServiceMock.Setup(x => x.RequestEMailAsync(It.IsAny<MailRequest>(), It.IsAny<CancellationToken>()))
            .Callback<MailRequest, CancellationToken>((mailRequest, cancellationToken) => { actualMailRequest = mailRequest; });

        //Act
        await UserManager.ResendConfirmEmail(AuditUserDetails, user, CancellationToken.None);

        //Assert

        //Assert that the e-mail request was sent.
        MailServiceMock.Verify(x => x.RequestEMailAsync(It.IsAny<MailRequest>(), It.IsAny<CancellationToken>()), Times.Once);
        Assert.That(actualMailRequest, Is.Not.Null);
        Assert.That(actualMailRequest.EmailType, Is.EqualTo(MailType.ConfirmEmailAddress));
        Assert.That(actualMailRequest.To.Count, Is.EqualTo(1));
        Assert.That(actualMailRequest.To[0].DisplayName, Is.EqualTo("Audit Tester"));
        Assert.That(actualMailRequest.To[0].Email, Is.EqualTo("audit@test.test"));
        Assert.That(actualMailRequest.Parameters.Count, Is.EqualTo(1));
        Assert.That(actualMailRequest.Parameters["url"], Is.Not.Empty);
    }
}