package com.sentai.demo;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
  private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

  @GetMapping("/demo/hello")
  public Map<String, Object> hello() {
    Map<String, Object> payload = new LinkedHashMap<String, Object>();
    payload.put("ok", true);
    payload.put("message", "hello from java demo");
    payload.put("now", Instant.now().toString());
    return payload;
  }

  @GetMapping("/demo/error")
  public Map<String, Object> error(@RequestParam(defaultValue = "demo failure") String message) {
    try {
      throw new IllegalStateException(message);
    } catch (IllegalStateException error) {
      LOGGER.error("demo endpoint raised an exception", error);
    }

    Map<String, Object> payload = new LinkedHashMap<String, Object>();
    payload.put("ok", false);
    payload.put("logged", true);
    payload.put("message", message);
    payload.put("now", Instant.now().toString());
    return payload;
  }
}
