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

namespace eSuite.API.UnitTests.Controllers.ProfileControllerUnitTests;

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

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

        AddAuthorisedUserToController(id, email, displayName);

        var userProfile = new UserProfile
        {
            Email = email
        };

        _userManagerMock.Setup(x => x.GetProfile(email, It.IsAny<CancellationToken>())).Returns(Task.FromResult(userProfile));

        //Act
        var actualResult = await _profileController.GetMyProfile();

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

        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
        Assert.That(objectResult?.Value, Is.Not.Null);

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(UserProfile)));

            var problemDetails = objectResult.Value as UserProfile;

            Assert.That(problemDetails?.Email, Is.EqualTo(userProfile.Email));
        }
    }

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

        AddAuthorisedUserToController(id, email, displayName);

        UserProfile? userProfile = null;

        _userManagerMock.Setup(x => x.GetProfile(email, It.IsAny<CancellationToken>())).Returns(Task.FromResult(userProfile)!);

        //Act
        var actualResult = await _profileController.GetMyProfile();

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

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

        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType, Is.EqualTo(typeof(UserProfile)));

            var problemDetails = objectResult.Value as UserProfile;

            Assert.That(problemDetails?.Email, Is.EqualTo(userProfile?.Email));
        }
    }
}