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.ProfileControllerUnitTests;

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

    [Test]
    public void EditMyProfile_EmailExistsExists_ReturnsBadRequest()
    {
        //Arrange
        const long id = -1;
        const string email = "test@test.test";
        const string displayName = "Testy McTester";
        const string errorText = "Test error message";

        AddAuthorisedUserToController(id, email, displayName);

        var userProfile = new UpdatedUserProfile
        {
            Email = "duplicate@test.test"
        };

        _userManagerMock.Setup(x => x.UpdateProfile(It.IsAny<AuditUserDetails>(), email, userProfile, It.IsAny<CancellationToken>())).Throws(new ExistsException(errorText));

        //Assert
        Assert.ThrowsAsync<ExistsException>(async () =>
        {
            //Act
            var actualResult = await _profileController.EditMyProfile(userProfile);
        });
    }

    [Test]
    public async Task EditMyProfile_EverythingOk_ReturnsOk()
    {
        //Arrange
        const long Id = -1;
        const string email = "test@test.test";
        const string displayName = "Testy McTester";

        AddAuthorisedUserToController(Id, email, displayName);

        var userProfile = new UpdatedUserProfile
        {
            Email = "newemail@test.test"
        };

        //Act
        var actualResult = await _profileController.EditMyProfile(userProfile);

        //Assert
        _userManagerMock.Verify(x => x.UpdateProfile(It.IsAny<AuditUserDetails>(), email, userProfile, It.IsAny<CancellationToken>()), Times.Once());

        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkResult)));
        var objectResult = actualResult as OkResult;

        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
    }
}