package com.ffii.core.exception;

import java.util.Map;

import jakarta.validation.constraints.NotNull;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/* sub record not found (e.g. item_line record) */
public class UnprocessableEntityException extends ResponseStatusException {

  public UnprocessableEntityException() {
    super(HttpStatus.UNPROCESSABLE_ENTITY);
  }

  public UnprocessableEntityException(@NotNull Map<String, Object> map) {
    super(HttpStatus.UNPROCESSABLE_ENTITY, map2Str(map));
  }

  public UnprocessableEntityException(String reason) {
    super(HttpStatus.UNPROCESSABLE_ENTITY, reason);
  }

  private static String map2Str(@NotNull Map<String, Object> map) {
    try {
      return new ObjectMapper().writeValueAsString(map);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
      return "";
    }
  }

}
