using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
using e_suite.Database.Audit;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.UserManagerControllerUnitTests;

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

    [Test]
    public void CreateUser_UserAlreadyExists_Error()
    {
        //Arrange
        var userRegistration = new UserRegistration
        {
            Email = "test@testy.test"
        };

        _userManagerMock.Setup(x => x.CreateUser(It.IsAny<AuditUserDetails>(), userRegistration, It.IsAny<CancellationToken>())).Throws(new ExistsException());

        //Assert
        Assert.ThrowsAsync<ExistsException>(async () =>
        {
            //Act
            var actualResult = await _userManagerController.CreateUser(userRegistration);
        });
    }

    [Test]
    public async Task CreateUser_UserDoesNotExist_ReturnsOK()
    {
        //Arrange
        var userRegistration = new UserRegistration
        {
            Email = "test@testy.test"
        };

        //Act
        var actualResult = await _userManagerController.CreateUser(userRegistration);

        //Assert
        _userManagerMock.Verify(x => x.CreateUser(It.IsAny<AuditUserDetails>(), userRegistration, It.IsAny<CancellationToken>()), Times.Once);

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

    [Test]
    public void CreateUser_BadRequest_ReturnError()
    {
        //Arrange
        var userRegistration = new UserRegistration
        {
            Email = "test@testy.test"
        };

        var message = "This is a test message";

        _userManagerMock.Setup(x => x.CreateUser(It.IsAny<AuditUserDetails>(), userRegistration, It.IsAny<CancellationToken>())).Throws(new ArgumentException(message));

        //Assert
        Assert.ThrowsAsync<ArgumentException>(async () =>
        {
            //Act
            var actualResult = await _userManagerController.CreateUser(userRegistration);
        });
    }
}