package com.banesco.common.infrastructure.config;

import com.banesco.module.security_notification.domain.model.SecurityNotificationConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.Config;

import java.util.Map;

@Slf4j
@ApplicationScoped
@RegisterForReflection
public class RestClientConfig {

    private final Config config;
    private final ObjectMapper objectMapper;

    private static final String API_BASE = "api.rest-client.";
    private static final String API_SECURITY_NOTIFICATION_NAME = "security-notification";

    @Inject
    public RestClientConfig(
        Config config,
        ObjectMapper objectMapper
    ) {
        this.config = config;
        this.objectMapper = objectMapper;
    }

    public SecurityNotificationConfig getSecurityNotificationConfig() {
        return getConfig(API_SECURITY_NOTIFICATION_NAME, SecurityNotificationConfig.class);
    }

    private <T> T getConfig(
        String configName,
        Class<T> configType
    ) {
        try {
            String fullConfigName = API_BASE + configName;
            String json = config.getValue(fullConfigName, String.class);
            log.info("Configurando {}: {}", fullConfigName, json);

            if (json == null || json.trim().isEmpty()) {
                throw new IllegalStateException("Configuracion no encontrada para: " + fullConfigName);
            }

            Map<String, Object> configMap = objectMapper.readValue(json, new TypeReference<>() {});
            return objectMapper.convertValue(configMap, configType);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("Formato JSON invalido para " + configName + ": " + e.getMessage(), e);
        } catch (Exception e) {
            throw new IllegalStateException("Error cargando configuracion del servicio " + configName + ": " + e.getMessage(), e);
        }
    }
}