package com.banesco.common.application.helper;

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.common.infrastructure.config.RequestValidationConfig;
import com.banesco.module.payment_initiation_request.domain.dto.request.PaymentInitiationRequestRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

@Slf4j
@ApplicationScoped
public class RequestValidatorHelper {
    private final RequestValidationConfig config;

    private static final String REQUIRED_CODE = "VDE01";
    private static final String VALIDATE_CODE = "VDE02";

    @Inject
    public RequestValidatorHelper(
        RequestValidationConfig config
    ) {
        this.config = config;
    }

    public void validateRequired(
        PaymentInitiationRequestRequest request
    ) {
        required(request.getPaymentTransactionType(), "paymentInitiationTransaction.paymentTransactionType");
        required(request.getPayerId(), "paymentInitiationTransaction.payerReference.partyIdentification[0].partyIdentification.identifierValue");
        required(request.getPayeeId(), "paymentInitiationTransaction.payeeReference.partyIdentification[0].partyIdentification.identifierValue");
        required(request.getAmount(), "paymentInitiationTransaction.amount");
        required(request.getCurrency(), "paymentInitiationTransaction.currencyCode");
        required(request.getPurpose(), "paymentInitiationTransaction.paymentPurpose");
        required(request.getPurpose(), "paymentInitiationTransaction.paymentPurpose");
        required(request.getChannelCode(), "procedureRequest.instructionPurposeType");
        required(request.getCustomerReferenceFintechId(), "customerReferenceFintechId");
        required(request.getAppId(), "appId");
    }

    public void validateFieldValues(
        PaymentInitiationRequestRequest request
    ) {
        validate(request.getPayerId(), config.applicantIdNumber(), "paymentInitiationTransaction.payerReference.partyIdentification[0].partyIdentification.identifierValue");
        validate(request.getPayeeId(), config.recipientIdNumber(), "paymentInitiationTransaction.payeeReference.partyIdentification[0].partyIdentification.identifierValue");
        validate(request.getAmount(), "paymentInitiationTransaction.amount");
        validate(request.getChannelCode(), config.channelCode(), "procedureRequest.instructionPurposeType");
    }

    private void required(String value, String fieldName) {
        if (value == null || value.trim().isEmpty()) {
            throw HttpStatusCodeException.badRequest(REQUIRED_CODE, fieldName);
        }
    }

    private void required(BigDecimal value, String fieldName) {
        if (value == null) {
            throw HttpStatusCodeException.badRequest(REQUIRED_CODE, fieldName);
        }
    }

    private void validate(String value, String regex, String fieldName) {
        if (value != null && !value.isEmpty() && !value.matches(regex)) {
            throw HttpStatusCodeException.badRequest(VALIDATE_CODE, fieldName);
        }
    }

    private void validate(BigDecimal value, String fieldName) {
        if (value != null && value.compareTo(BigDecimal.ZERO) < 1) {
            throw HttpStatusCodeException.badRequest(VALIDATE_CODE, fieldName);
        }
    }
}
