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

namespace eSuite.API.UnitTests.Controllers.AccountControllerUnitTests;

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

    [Test]
    public async Task LoginGet_WhenLoginNotPresent_ReturnsLoginViewWithEmptyModel()
    {
        //Arrange
        _cookieManagerMock.Setup(x => x.GetSsoIdFromSsoIdCookie(It.IsAny<HttpRequest>())).ReturnsAsync(() => null);

        var userId = 99;
        var email = "email@mail.test";
        var displayName = "Testy McTester";
        AddAuthorisedUserToController(userId, email, displayName);

        var userProfile = new UserProfile
        {
            Email = "Test@test.test",
            FirstName = "Testy",
            MiddleNames = "The tested",
            LastName = "McTester",
            SsoProviderId = 2
        };

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

        //Act
        var response = await _accountController.ProfileGet(CancellationToken.None);

        //Assert
        Assert.That(response, Is.TypeOf<PartialViewResult>());
        var viewResult = response as PartialViewResult;
        Assert.That(viewResult?.ViewName, Is.EqualTo("Profile"));

        Assert.That(viewResult?.Model, Is.TypeOf<ProfileViewModel>());
        var profileViewModel = viewResult!.Model as ProfileViewModel;

        Assert.That(profileViewModel.Profile, Is.TypeOf<Models.UserProfile>());

        var actualProfile = profileViewModel.Profile;
        Assert.That(actualProfile, Is.Not.Null);
        Assert.That(actualProfile?.Email, Is.EqualTo(userProfile.Email));
        Assert.That(actualProfile?.FirstName, Is.EqualTo(userProfile.FirstName));
        Assert.That(actualProfile?.MiddleNames, Is.EqualTo(userProfile.MiddleNames));
        Assert.That(actualProfile?.LastName, Is.EqualTo(userProfile.LastName));
        Assert.That(actualProfile?.SsoProviderId, Is.EqualTo(userProfile.SsoProviderId));
    }
}