package com.banesco.module.security.infrastructure.client;

import com.banesco.common.application.usecase.HttpClientUseCase;
import com.banesco.common.domain.exception.ServiceUnavailableException;
import com.banesco.common.domain.model.ApiResponse;
import com.banesco.common.domain.model.HttpRequest;
import com.banesco.common.infrastructure.config.RestClientConfig;
import com.banesco.module.security.application.usecase.SecurityUseCase;
import com.banesco.module.security.domain.dto.request.SecurityRequest;
import lombok.extern.slf4j.Slf4j;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@ApplicationScoped
public class RegisterSecurityClient implements SecurityUseCase {
    private final HttpClientUseCase httpClientUseCase;
    private final RestClientConfig.RegisterSecurityConfig securityConfig;

    @Inject
    public RegisterSecurityClient(
        HttpClientUseCase httpClientUseCase,
        RestClientConfig restClientConfig
    ) {
        this.httpClientUseCase = httpClientUseCase;
        this.securityConfig = restClientConfig.getRegisterSecurityConfig();
        log.info("Configuración cargada para register-security: {}", securityConfig);
    }

    @Override
    public <T> ApiResponse<T> execute(Class<T> responseType) {
        log.info("Registrando traza de seguridad");

        try {
            SecurityRequest requestBody = createSecurityRequestBody();
            Map<String, String> headers = createHeaders();
            HttpRequest request = buildHttpRequest(requestBody, headers, responseType);

            log.debug("Request body: {}", requestBody);
            log.debug("Request configurado: {}", request);

            return httpClientUseCase.execute(request);

        } catch (Exception e) {
            log.error("Error registrando traza de seguridad: {}", e.getMessage(), e);
            throw new ServiceUnavailableException("503", e.getMessage(), null);
        }
    }

    private SecurityRequest createSecurityRequestBody() {
        return SecurityRequest.builder()
            .codBan(securityConfig.getConfig().getBankCod())
            .codMon(securityConfig.getConfig().getCurCod())
            .codEve(securityConfig.getConfig().getEventCod())
            .codEve2(securityConfig.getConfig().getEventCod())
            .sp(securityConfig.getConfig().getSp())
            .build();
    }

    private Map<String, String> createHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        return headers;
    }

    private HttpRequest buildHttpRequest(SecurityRequest body, Map<String, String> headers, Class<?> responseType) {
        return HttpRequest.builder()
            .url(securityConfig.getUrl())
            .method(HttpRequest.HttpMethod.POST)
            .body(body)
            .headers(headers)
            .responseType(responseType)
            .connectTimeout(securityConfig.getTimeout().getConnect())
            .readTimeout(securityConfig.getTimeout().getResponse())
            .build();
    }


}