package com.hpay.hpay_mobile_api.utils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;


public class PasswordUtils {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final SecureRandom secureRandom = new SecureRandom();


    // :cadenas_fermé_et_clé: Génère un sel alphanumérique sécurisé (64 caractères)
    public static String generateSalt(int length) {
        StringBuilder salt = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            salt.append(CHARACTERS.charAt(secureRandom.nextInt(CHARACTERS.length())));
        }
        return salt.toString();
    }


    // :cadenas: Hash (SHA-512) du mot de passe + sel, retourne en hexadécimal (identique à PHP)
    public static String hashPassword(String password, String salt) {
        try {
            String input = password + salt;
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            byte[] hashedBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // Conversion en HEXA comme hash() en PHP
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Erreur de hashage SHA-512", e);
        }
    }



    // :coche_blanche: Vérifie que le mot de passe entré correspond au hash stocké
    public static boolean verifyPassword(String inputPassword, String storedSalt, String storedHash) {
        String computedHash = hashPassword(inputPassword, storedSalt);
        return computedHash.equals(storedHash);
    }




    /*
        private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        private static final SecureRandom secureRandom = new SecureRandom();


        // :cadenas_fermé_et_clé: Génère un sel de 64 caractères alphanumériques
        public static String generateSalt(int length) {
            StringBuilder salt = new StringBuilder(length);
            for (int i = 0; i < length; i++) {
                salt.append(CHARACTERS.charAt(secureRandom.nextInt(CHARACTERS.length())));
            }
            return salt.toString();
        }

        // :cadenas: Hash le mot de passe + sel en SHA-512 et encode en Base64
        /*public static String hashPassword(String password, String salt) {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-512");
                byte[] hashBytes = md.digest((password + salt).getBytes());
                return Base64.getEncoder().encodeToString(hashBytes);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Erreur de hashage SHA-512", e);
            }
        }



        public static String hashPassword(String password) {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-512");
                byte[] hashedBytes = md.digest(password.getBytes("UTF-8"));
                return Base64.getEncoder().encodeToString(hashedBytes);
            } catch (Exception e) {
                throw new RuntimeException("Erreur de hashage SHA-512", e);
            }
        }

        public static String hashPassword(String password, String salt) {
            try {
                String input = password + salt;
                MessageDigest md = MessageDigest.getInstance("SHA-512");
                byte[] hashedBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
                return Base64.getEncoder().encodeToString(hashedBytes);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Erreur de hashage SHA-512", e);
            }
        }


     */

        //:coche_blanche: Vérifie si le mot de passe correspond au hash stocké
      /*  public static boolean verifyPassword(String inputPassword, String storedSalt, String storedHash) {
            String computedHash = hashPassword(inputPassword + storedSalt);
            return computedHash.equals(storedHash);
        }*/


}
