using System.Diagnostics;
using System.Text;
using PlanTempus.Core;
using Sodium;

namespace PlanTempus.X.TDD;

[TestClass]
public class PasswordHasherTests : TestFixture
{
    [TestMethod]
    public void MyTestMethod()
    {
        var stopwatch = Stopwatch.StartNew();

        var salt = PasswordHash.ScryptGenerateSalt();

        // 2. Konverter password til byte[]
        var passwordBytes = Encoding.UTF8.GetBytes("password123");

        // 3. Kald ScryptHashBinary korrekt
        var hash = PasswordHash.ScryptHashBinary(
            passwordBytes,
            salt
        );

        stopwatch.Stop();
    }

    [TestMethod]
    public void HashPassword_ShouldCreateValidHashFormat()
    {
        // Arrange
        var password = "TestPassword123";

        // Act
        var hashedPassword = new SecureTokenizer().TokenizeText(password);
        var parts = hashedPassword.Split('.');

        // Assert
        Assert.AreEqual(3, parts.Length);
        Assert.AreEqual("100000", parts[0]);
    }

    [TestMethod]
    public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
    {
        // Arrange
        var password = "TestPassword123";
        var hashedPassword = new SecureTokenizer().TokenizeText(password);

        // Act
        var result = new SecureTokenizer().VerifyToken(hashedPassword, password);

        // Assert
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
    {
        // Arrange
        var correctPassword = "TestPassword123";
        var wrongPassword = "WrongPassword123";
        var hashedPassword = new SecureTokenizer().TokenizeText(correctPassword);

        // Act
        var result = new SecureTokenizer().VerifyToken(hashedPassword, wrongPassword);

        // Assert
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
    {
        // Arrange
        var password = "TestPassword123";
        var invalidHash = "InvalidHash";

        // Act
        var result = new SecureTokenizer().VerifyToken(invalidHash, password);

        // Assert
        Assert.IsFalse(result);
    }
}