using System.Security.Cryptography;
using System.Text;

namespace Yuna.Website.Server.Infrastructure
{
    public class Encrypter
    {
        private static HMACSHA256 _passwordHasher = new() { Key = [2, 2, 2, 2, 2, 2, 22, 2, 2, 2, 2, 2, 2, 2] };
        private static HMACSHA256 _tokenHasher = new() { Key = [21, 12, 21, 21, 2, 11, 111, 2, 2, 21, 2, 21, 2, 2] };
        public static string HashPassword(string password, string username)
        {

            var loweredUsername = username.ToLower();

            var hashedPassword = Convert.ToBase64String(_passwordHasher.ComputeHash(Encoding.UTF8.GetBytes(password + loweredUsername)))
                ?? throw new ArgumentNullException("didnt manage to generate password");
            return hashedPassword;
        }

        public static byte[] CreateTokenSalt(long userId, DateTime creationTime)
        {
            return Encoding.UTF8.GetBytes(userId.ToString() + creationTime.ToString("dd_mm_yy___hh_ss"));
        }

        public static string HashTokenSalt(byte[] bytesStr)
        {
            var hashedSalt = _tokenHasher.ComputeHash(bytesStr);
            return Convert.ToBase64String(hashedSalt);
        }

        public static string GenerateRandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            Random random = new Random();
            return new string(Enumerable.Repeat(chars, length)
                .Select(s => s[random.Next(s.Length)]).ToArray());
        }
    }
}
