package com.psg.dlsysinfo.dl_sysinfo_server.controller;

import com.psg.dlsysinfo.dl_sysinfo_server.config.PingToggle;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api")
public class HealthCheckController {

    private final PingToggle pingToggle;

    public HealthCheckController(PingToggle pingToggle) {
        this.pingToggle = pingToggle;
    }

    @GetMapping("/system/ping-status")
    public ResponseEntity<Map<String, Boolean>> pingStatus() {
        return ResponseEntity.ok(Map.of("acceptPings", pingToggle.isAcceptPings()));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/admin/toggle-ping")
    public ResponseEntity<String> togglePing(@RequestParam boolean enabled, @AuthenticationPrincipal Object user) {
        pingToggle.setAcceptPings(enabled);
        return ResponseEntity.ok("Ping is now " + (enabled ? "ENABLED" : "DISABLED"));
    }
}
