using e_suite.API.Common.exceptions;
using e_suite.Database.Audit;
using eSuite.API.Models;
using eSuite.Core.Miscellaneous;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.UserManagerControllerUnitTests;

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

    [Test]
    public async Task DeactivateUser_ActiveUser_ReturnsOK()
    {
        //Arrange
        var email = new EmailAddress
        {
            Email = "deactivate.me@sun-strategy.com"
        };

        //Act
        var actualResult = await _userManagerController.DeactivateUser(email);

        //Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkResult)));

        _userManagerMock.Verify(x => x.DeactivateUser(It.IsAny<AuditUserDetails>(), email.Email, It.IsAny<CancellationToken>()), Times.Once);
    }

    [Test]
    public void DeactivateUser_UserNotFound_Returns404()
    {
        //Arrange
        var email = new EmailAddress
        {
            Email = "deactivate.me@sun-strategy.com"
        };

        _userManagerMock.Setup(x => x.DeactivateUser(It.IsAny<AuditUserDetails>(), email.Email, It.IsAny<CancellationToken>())).Throws(() => new NotFoundException());

        //Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var actualResult = await _userManagerController.DeactivateUser(email);
        });
    }

    [Test]
    public void DeactivateUser_UserAlreadyDeactivated_ReturnsBadRequest()
    {
        //Arrange
        var email = new EmailAddress
        {
            Email = "deactivate.me@sun-strategy.com"
        };

        _userManagerMock.Setup(x => x.DeactivateUser(It.IsAny<AuditUserDetails>(), email.Email, It.IsAny<CancellationToken>())).Throws(() => new InvalidOperationException());

        //Assert
        Assert.ThrowsAsync<InvalidOperationException>(async () =>
        {
            //Act
            var actualResult = await _userManagerController.DeactivateUser(email);
        });
    }

    [Test]
    public async Task DeactivateUserByGeneralIdRef_UserAlreadyDeactivated_ReturnsBadRequest()
    {
        //Arrange
        var cancellationToken = CancellationToken.None;

        var user = new GeneralIdRef();

        //Act
        var actualResult = await _userManagerController.DeactivateUser(user, cancellationToken);

        //Assert
        _userManagerMock.Verify(x => x.DeactivateUser(It.IsAny<AuditUserDetails>(), user, cancellationToken), Times.Once);
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkResult)));
    }
}