package com.spring.common.error;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.validation.FieldError;

import lombok.Getter;

@Getter
public class BizErrorResponse extends ErrorResponse {

    public BizErrorResponse(ErrorRule exceptionRule, List<RejectedValue> errors) {
        super(exceptionRule);
        this.errors = errors;
    }

    public static BizErrorResponse valueOf(ErrorRule exceptionRule) {
        return new BizErrorResponse(exceptionRule, null);
    }

    public static BizErrorResponse fromFieldError(List<FieldError> fieldErrors) {
        List<RejectedValue> errors = fieldErrors.stream()
            .map(fieldError -> RejectedValue.of(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage()))
            .collect(Collectors.toList());
        return new BizErrorResponse(ExceptionRule.UNPROCESSABLE_ENTITY, errors);
    }

}
