using API.Models.Internal.User;

namespace API.Tests.Mappers;

public class RegistrationKeyMapperTests
{
    [Fact]
    public void ToInternalFromIngoing_MapsLinkedRole()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = "Admin"
        };

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        Assert.NotNull(result);
        Assert.Equal("Admin", result.LinkedRole);
    }

    [Fact]
    public void ToInternalFromIngoing_NullLinkedRole_MapsCorrectly()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = null
        };

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        Assert.NotNull(result);
        Assert.Null(result.LinkedRole);
    }

    [Fact]
    public void ToInternalFromIngoing_GeneratesNewId()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = "User"
        };

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        Assert.NotNull(result.Id);
        Assert.NotEmpty(result.Id);
    }

    [Fact]
    public void ToInternalFromIngoing_GeneratesUniqueIds()
    {
        // Arrange
        var ingoing1 = new RegistrationKeyIngoing { LinkedRole = "Admin" };
        var ingoing2 = new RegistrationKeyIngoing { LinkedRole = "User" };

        // Act
        var result1 = ingoing1.ToInternalFromIngoing();
        var result2 = ingoing2.ToInternalFromIngoing();

        // Assert
        Assert.NotEqual(result1.Id, result2.Id);
    }

    [Fact]
    public void ToInternalFromIngoing_SetsCreatedDate()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = "Moderator"
        };
        var beforeCreation = DateTime.UtcNow;

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        var afterCreation = DateTime.UtcNow;
        Assert.True(result.Created >= beforeCreation.AddSeconds(-1));
        Assert.True(result.Created <= afterCreation.AddSeconds(1));
    }

    [Fact]
    public void ToInternalFromIngoing_EmptyLinkedRole_MapsCorrectly()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = ""
        };

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        Assert.Equal("", result.LinkedRole);
    }

    [Fact]
    public void ToInternalFromIngoing_WhitespaceLinkedRole_MapsCorrectly()
    {
        // Arrange
        var ingoing = new RegistrationKeyIngoing
        {
            LinkedRole = "   "
        };

        // Act
        var result = ingoing.ToInternalFromIngoing();

        // Assert
        Assert.Equal("   ", result.LinkedRole);
    }
}
