package com.psg.dlsysinfo.dl_sysinfo_server.db;

import com.psg.dlsysinfo.dl_sysinfo_server.error.ApiError;
import org.springframework.jdbc.core.JdbcTemplate;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DatabaseService {

    private final JdbcTemplate jdbcTemplate;
    private static final int CURRENT_GROUP_VERSION = 2;
    private static final String SHARED_MEMBER = "SharedMember"; // Define your shared member constant

    public DatabaseService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    private String hashToken(String token, String groupName) throws ApiError {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            String input = token + groupName; // Combine token and group name for hashing
            byte[] hashBytes = digest.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();

            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString(); // Return the hashed token as a hex string
        } catch (NoSuchAlgorithmException e) {
            throw new ApiError("Hashing algorithm not found", e);

        }
    }

    // ... (rest of your methods remain unchanged)
}
