package com.tsi.api.server.error;

import lombok.Builder;
import lombok.Getter;

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

@Getter
public class TscSsipApiErrorResponse {
    private final int code;
    private final String message;
    private List<FieldError> errors;

    @Builder
    public TscSsipApiErrorResponse(int code, String message, List<FieldError> errors) {
        this.code    = code;
        this.message = message;
        this.errors  = initErrors(errors);
    }

    private List<FieldError> initErrors(List<FieldError> errors) {
        return (errors == null) ? new ArrayList<>() : errors;
    }

    public static TscSsipApiErrorResponse buildError(TscSsipApiErrorCode errorCode) {
        return TscSsipApiErrorResponse.builder()
                .code(errorCode.getCode())
                .message(errorCode.getMessage())
                .build();
    }

    @Getter
    public static class FieldError {
        private final String field;
        private final String value;
        private final String reason;

        @Builder
        public FieldError(String field, String value, String reason) {
            this.field = field;
            this.value = value;
            this.reason = reason;
        }
    }

}
