package com.banesco.common.application.helper;

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

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

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

    public void validateRequired(
        LegalCustomerProductDirectoryRequest request
    ) {
        required(request.getCustomerId(), "customerId");
        required(request.getCustomerReferenceFintechId(), "customerReferenceFintechId");
        required(request.getAppId(), "appId");
    }

    public void validateFieldValues(
        LegalCustomerProductDirectoryRequest request
    ) {
        validate(request.getCustomerId(), config.customerId(), "customerId");
        validate(request.getAccountStatus(), config.accountStatus(), "accountStatus");
        validate(request.getProductCvCode(), config.productCvCode(), "productCvCode");
        validate(request.getLimitType(), config.limitType(), "limitType");
        validate(request.getCasheaIndicator(), config.cacheaIndicator(), "casheaIndicator");
    }

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

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