using e_suite.API.Common;
using e_suite.API.Common.models;
using e_suite.Database.Core.Tables.UserManager;
using Moq;
using NUnit.Framework;
using UserManager.UnitTests.Helpers;

namespace UserManager.UnitTests.UserManager;

[TestFixture]
public class PatchProfileUnitTests : UserManagerTestBase<PatchUserProfile>
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task PatchProfile_AppliesPatchToUser()
    {
        // Arrange
        var dto = new PatchUserProfile
        {
            FirstName = "Updated"
        };

        const string existingEmail = "testuser@sun-strategy.com";
        var existingUser = new User
        {
            Id = 12,
            Email = existingEmail
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);
        
        PatchMock
            .Setup(p => p.ApplyTo(It.IsAny<User>()))
            .Callback<object>(target =>
            {
                ((User)target).FirstName = "Updated";
            });

        // Act
        await UserManager.PatchProfile(AuditUserDetails, existingUser.Email, dto, CancellationToken.None);

        // Assert
        PatchFactoryMock.Verify(f => f.Create(dto), Times.Once);
        PatchMock.Verify(p => p.ApplyTo(It.IsAny<User>()), Times.Once);

        Assert.That(existingUser.FirstName, Is.EqualTo("Updated"));
    }
}