using e_suite.API.Common.exceptions;
using eSuite.API.Models;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.AuthenticationControllerUnitTests;

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

    [Test]
    public async Task ForgotPassword_EmailFound_ReturnsOK()
    {
        //Arrange
        var forgotPasswordRequest = new EmailAddress
        {
            Email = "test@test.test"
        };

        //Act
        var actualResult = await _authenticationController.ForgotPassword(forgotPasswordRequest);

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

    [Test]
    public async Task ForgotPassword_SentinelGetsInvolved_ReturnsSentinelsResult()
    {
        //Arrange
        var forgotPasswordRequest = new EmailAddress
        {
            Email = "test@test.test"
        };

        _sentinelMock.Setup(x => x.CheckSecurity(_authenticationController, It.IsAny<CancellationToken>())).Returns<ControllerBase, CancellationToken>((controller, cancellationToken) => Task.FromResult<IActionResult?>(controller.Unauthorized()));

        //Act
        var actualResult = await _authenticationController.ForgotPassword(forgotPasswordRequest);

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

    [Test]
    public void ForgotPassword_EmailNotFound_ReturnsBadRequest()
    {
        //Arrange
        var forgotPasswordRequest = new EmailAddress
        {
            Email = "test@test.test"
        };

        _userManagerMock.Setup(x => x.ForgotPassword(forgotPasswordRequest.Email, It.IsAny<CancellationToken>())).Throws(new NotFoundException());

        //Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var actualResult = await _authenticationController.ForgotPassword(forgotPasswordRequest);
        });
    }

    [Test]
    public async Task ForgotPassword_EmailNotConfirmed_ReturnsBadRequest()
    {
        //Arrange
        var forgotPasswordRequest = new EmailAddress
        {
            Email = "test@testy.test"
        };
        
        _userManagerMock.Setup(x => x.ForgotPassword(forgotPasswordRequest.Email, It.IsAny<CancellationToken>())).Throws(new EmailNotConfirmedException());

        //Act
        var actualResult = await _authenticationController.ForgotPassword(forgotPasswordRequest);

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