package application.service;

import application.port.input.IValidateOtp;
import application.port.input.command.ValidateOtpCommand;
import com.banesco.common.infraestructure.helpers.LoggerHelper;
import infrastructure.config.DomainConfig;
import infrastructure.config.OtpConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApplicationScoped
public class OtpValidationService {

    private final IValidateOtp validateOtpUseCase;
    private final OtpConfig otpConfig;
    private final DomainConfig domainConfig;

    @Inject
    public OtpValidationService(
            IValidateOtp validateOtpUseCase,
            OtpConfig otpConfig,
            DomainConfig domainConfig
    ) {
        this.validateOtpUseCase = validateOtpUseCase;
        this.otpConfig = otpConfig;
        this.domainConfig = domainConfig;
    }

    /**
     * Punto de entrada principal para validar un OTP.
     *
     * @return {@code true} si el OTP es válido, de lo contrario {@code false}.
     */
    public boolean isOtpValid(
            String otpToken,
            String originApp,
            String login,
            String ipAddress,
            String requestId
    ) {
        return validateOtpFromApiService(otpToken, originApp, login, ipAddress, requestId);
    }

    private boolean validateOtpFromApiService(
            String otpToken,
            String originApp,
            String login,
            String ipAddress,
            String requestId
    ) {
        log.info(LoggerHelper.buildInfoPrivateRequest(requestId, "Validando OTP usando servicio externo"));

        var otpCommand = new ValidateOtpCommand(
                otpToken,
                originApp,
                login,
                ipAddress,
                otpConfig.channel(),
                otpConfig.servicio(),
                domainConfig.payment()
        );

        return validateOtpUseCase.isValidOtp(otpCommand, requestId);
    }
}