package ru.soune.nocopy.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("check/api")
public class HealtCheckController {

    @GetMapping("/healt")
    public HttpStatus healtCheck() {
        return HttpStatus.OK;
    }

    @Value("${BUILD_TIME_BACK:unknown}")
    private String buildTimeBack;

    @GetMapping("/build")
    public BuildInfo getBuildInfo() {
        return new BuildInfo(buildTimeBack);
    }

    @GetMapping("/api/debug/memory")
    public Map<String, String> getMemoryInfo() {
        Runtime runtime = Runtime.getRuntime();
        long mb = 1024 * 1024;

        Map<String, String> info = new HashMap<>();
        info.put("maxMemory (MB)", String.valueOf(runtime.maxMemory() / mb));
        info.put("totalMemory (MB)", String.valueOf(runtime.totalMemory() / mb));
        info.put("freeMemory (MB)", String.valueOf(runtime.freeMemory() / mb));
        info.put("usedMemory (MB)", String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / mb));
        info.put("availableProcessors", String.valueOf(runtime.availableProcessors()));

        info.put("JAVA_TOOL_OPTIONS", System.getenv("JAVA_TOOL_OPTIONS"));
        info.put("JAVA_OPTS", System.getenv("JAVA_OPTS"));

        return info;
    }

    static class BuildInfo {
        public String buildTimeBack;

        public BuildInfo(String buildTimeBack) {
            this.buildTimeBack = buildTimeBack;
        }
    }
}
