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

namespace Quasar.Server.Licensing
{
    public static class LicenseManager
    {
        private const string SECRET_KEY = "QuasarRAT_2024_SecretKey_DoNotShare";

        public static string GenerateHardwareID()
        {
            string pcName = Environment.MachineName;
            string randomPart = GenerateRandomString(8);
            return $"{pcName}-{randomPart}";
        }

        public static string GenerateBIN(string hardwareID)
        {
            using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(SECRET_KEY)))
            {
                byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(hardwareID));
                return Convert.ToBase64String(hash).Substring(0, 20).Replace("+", "").Replace("/", "").Replace("=", "");
            }
        }

        public static bool ValidateBIN(string hardwareID, string bin)
        {
            string expectedBIN = GenerateBIN(hardwareID);
            return string.Equals(expectedBIN, bin, StringComparison.Ordinal);
        }

        private static string GenerateRandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                result.Append(chars[random.Next(chars.Length)]);
            }
            return result.ToString();
        }
    }
}
