package com.psg.dlsysinfo.dl_sysinfo_server.service;

import com.psg.dlsysinfo.dl_sysinfo_server.entity.Client;
import com.psg.dlsysinfo.dl_sysinfo_server.repository.ClientRepository;
import com.psg.dlsysinfo.dl_sysinfo_server.security.EncryptionService;
import com.psg.dlsysinfo.dl_sysinfo_server.util.HashUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class ClientService {

    @Autowired
    private ClientRepository clientRepo;

    @Autowired
    private EncryptionService encryptionService;

    public Client registerClient(String clientName) {
        try {
            String identifier = UUID.randomUUID().toString();
            String encryptedName = encryptionService.encryptData(clientName);
            String hashedName = HashUtil.sha256(clientName);

            Client client = new Client(identifier, hashedName);
            client.setClientNameEncrypted(encryptedName);

            return clientRepo.save(client);
        } catch (Exception e) {
            throw new RuntimeException("Failed to encrypt client name", e);
        }
    }


}
