package com.banesco.common.application.helper;

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

@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(
        ServiceOrderStatusControlRequest request
    ) {
        required(request.getClientId(), "servicingOrderProcedureInstanceRecord.customerReference.partyIdentification[0].partyIdentification.identifierValue");
        required(request.getBankId(), "servicingOrderProcedureInstanceRecord.workProduct.productIdentification.identifierValue");
        required(request.getServiceCode(), "servicingOrderProcedureInstanceRecord.serviceOrderDescription");
        required(request.getServiceType(), "servicingOrderProcedureInstanceRecord.serviceOrderType");
        required(request.getServiceObserver(), "servicingOrderProcedureInstanceRecord.servicingOrderTask.task");
        required(request.getCustomerReferenceFintechId(), "customerReferenceFintechId");
        required(request.getAppId(), "appId");
    }

    public void validateFieldValues(
        ServiceOrderStatusControlRequest request
    ) {
        validate(request.getClientId(), config.customerIdNumber(), "servicingOrderProcedureInstanceRecord.customerReference.partyIdentification[0].partyIdentification.identifierValue");
        validate(request.getBankId(), config.bankId(), "servicingOrderProcedureInstanceRecord.workProduct.productIdentification.identifierValue");
        validate(request.getAccountNumber(), config.accountNumber(), "servicingOrderProcedureInstanceRecord.workProduct.productReference");
        validate(request.getServiceCode(), config.serviceCode(), "servicingOrderProcedureInstanceRecord.serviceOrderDescription");
        validate(request.getServiceType(), config.serviceType(), "servicingOrderProcedureInstanceRecord.serviceOrderType");
        validate(request.getServiceObserver(), config.serviceObserver(), "servicingOrderProcedureInstanceRecord.servicingOrderTask.task");}

    private void required(String value, String fieldName) {
        if (value == null || value.trim().isEmpty()) {
            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);
        }
    }
}
