package gc.mda.kcg.collector;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class CollectorStatusController {

    private final CollectorStatusTracker tracker;

    @GetMapping("/collector-status")
    public Map<String, Object> getStatus() {
        List<CollectorStatusTracker.CollectorStatus> statuses = tracker.getAll();
        return Map.of(
                "collectors", statuses.stream().map(s -> Map.of(
                        "name", s.getName(),
                        "region", s.getRegion() != null ? s.getRegion() : "",
                        "lastSuccess", s.getLastSuccess() != null ? s.getLastSuccess().toString() : "",
                        "lastFailure", s.getLastFailure() != null ? s.getLastFailure().toString() : "",
                        "lastCount", s.getLastCount(),
                        "lastError", s.getLastError() != null ? s.getLastError() : "",
                        "totalSuccess", s.getTotalSuccess(),
                        "totalFailure", s.getTotalFailure(),
                        "totalItems", s.getTotalItems()
                )).toList(),
                "serverTime", java.time.Instant.now().toString()
        );
    }
}
