using SIGCM2.Application.Common;

namespace SIGCM2.Application.Tests.Common;

public class TempPasswordGeneratorTests
{
    [Fact]
    public void Generate_Default_Length_Is_12()
    {
        var pwd = TempPasswordGenerator.Generate();
        Assert.Equal(12, pwd.Length);
    }

    [Fact]
    public void Generate_Always_Has_Uppercase_Letter()
    {
        for (int i = 0; i < 20; i++)
        {
            var pwd = TempPasswordGenerator.Generate();
            Assert.True(pwd.Any(char.IsUpper), $"No uppercase found in: {pwd}");
        }
    }

    [Fact]
    public void Generate_Always_Has_Lowercase_Letter()
    {
        for (int i = 0; i < 20; i++)
        {
            var pwd = TempPasswordGenerator.Generate();
            Assert.True(pwd.Any(char.IsLower), $"No lowercase found in: {pwd}");
        }
    }

    [Fact]
    public void Generate_Always_Has_Digit()
    {
        for (int i = 0; i < 20; i++)
        {
            var pwd = TempPasswordGenerator.Generate();
            Assert.True(pwd.Any(char.IsDigit), $"No digit found in: {pwd}");
        }
    }

    [Fact]
    public void Generate_Always_Has_Special_Char()
    {
        const string symbols = "!@#$%&*+-=?";
        for (int i = 0; i < 20; i++)
        {
            var pwd = TempPasswordGenerator.Generate();
            Assert.True(pwd.Any(c => symbols.Contains(c)), $"No symbol found in: {pwd}");
        }
    }

    [Fact]
    public void Generate_Below_8_Throws_ArgumentOutOfRangeException()
    {
        Assert.Throws<ArgumentOutOfRangeException>(() => TempPasswordGenerator.Generate(7));
    }

    [Fact]
    public void Generate_100_Samples_All_Pass_Diversity()
    {
        const string symbols = "!@#$%&*+-=?";
        for (int i = 0; i < 100; i++)
        {
            var pwd = TempPasswordGenerator.Generate(12);
            Assert.True(pwd.Length >= 12);
            Assert.Contains(pwd, char.IsUpper);
            Assert.Contains(pwd, char.IsLower);
            Assert.Contains(pwd, char.IsDigit);
            Assert.Contains(pwd, c => symbols.Contains(c));
        }
    }

    [Fact]
    public void Generate_Custom_Length_Respects_Length()
    {
        var pwd = TempPasswordGenerator.Generate(16);
        Assert.Equal(16, pwd.Length);
    }
}
