package com.banesco.common.application.helper;

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.module.msg_response_header.domain.model.AdditionalStatus;
import com.banesco.module.msg_response_header.domain.model.MsgRsStatus;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class ServiceStatusValidateHelper {

    private ServiceStatusValidateHelper() {}

    public static void validateStatus(MsgRsStatus status) {
        if(Objects.isNull(status)) {
            return;
        }

        List<String> errors = extractErrorCodes(status);

        if(!errors.isEmpty()) {
            AdditionalStatus additionalStatus = getFirstAdditionalStatus(status);
            String error = String.join("-", errors);
            String desc = (!Objects.isNull(additionalStatus))
                ? additionalStatus.getStatusDesc()
                : "";

            log.error(
                "Error asignado desde la lista concatenada de los errores: {} -> {}",
                error,
                desc
            );

            throw HttpStatusCodeException.badRequest(error, desc);
        } else {
            log.error(
                "Error asignado desde el status principal: {} -> {}",
                status.getStatusCode(),
                status.getStatusDesc()
            );

            throw HttpStatusCodeException.internalServer(
                status.getStatusCode(),
                status.getStatusDesc()
            );
        }
    }

    private static List<String> extractErrorCodes(MsgRsStatus status) {
        List<String> errors = new ArrayList<>();
        List<AdditionalStatus> additionalStatuses = status.getAdditionalStatus();

        if(Objects.isNull(additionalStatuses) || additionalStatuses.isEmpty()) {
            return errors;
        }

        for (AdditionalStatus item: additionalStatuses) {
            if (
                !Objects.equals(item.getStatusCode(), "000") &&
                !errors.contains(item.getStatusCode())
            ) {
                errors.add(item.getStatusCode());
            }
        }

        return errors;
    }

    private static AdditionalStatus getFirstAdditionalStatus(MsgRsStatus status) {
        List<AdditionalStatus> additionalStatuses = status.getAdditionalStatus();

        if(Objects.isNull(additionalStatuses) || additionalStatuses.isEmpty()) {
            return null;
        }

        for (AdditionalStatus item: additionalStatuses) {
            if (!Objects.equals(item.getStatusCode(), "000")) {
                return item;
            }
        }

        return null;
    }
}
