package com.hepl.tunefortwo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hepl.tunefortwo.config.i18n.Translator;
import com.hepl.tunefortwo.dto.GenericData;
import com.hepl.tunefortwo.dto.GenericResponse;
import com.hepl.tunefortwo.service.HashingService;
import com.hepl.tunefortwo.utils.AppMessages;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@Tag(name = "Manage Data Hashing", description = "Encryption and Decryption of data through hashing mechanism")
@SecurityRequirement(name = "Bearer Authentication")
@RestController
@RequestMapping("/v1/hashing")
@Slf4j
public class HashingController {

    private final Translator translator;
    private final HashingService hashingService;

    public HashingController(HashingService hashingService, Translator translator) {
        this.hashingService = hashingService;
        this.translator = translator;
    }

    @PostMapping("/encryption")
    public GenericResponse encryption(@RequestBody String plainData) throws Exception {
        log.info("encrypting the data: ${}", plainData);

        String encryptedData = hashingService.encryption(plainData);
        GenericResponse response = new GenericResponse(true);
        GenericData data = new GenericData();
        data.setEncryptedData(encryptedData);
        response.setMessage(translator.toLocale(AppMessages.DATA_ENCRYPTED));
        response.setData(data);

        return response;
    }

    @PostMapping("/decryption")
    public GenericResponse decryption(@RequestBody String encryptedData) throws Exception {
        log.info("decrypting the data: {}", encryptedData);

        String decryptedData = hashingService.decryption(encryptedData);
        GenericResponse response = new GenericResponse(true);
        GenericData data = new GenericData();
        data.setDecryptedData(decryptedData);
        response.setMessage(translator.toLocale(AppMessages.DATA_DECRYPTED));
        response.setData(data);

        return response;
    }
}
