package com.eactive.eai.manage.inflow;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage/inflow/group")
public class InflowGroupMetricController {

    @Autowired
    private InflowGroupMetricService metricService;

    /**
     * 특정 그룹의 메트릭 조회
     * GET /manage/inflow/group/{groupId}/metric
     */
    @GetMapping("/{groupId}/metric")
    public ResponseEntity<?> getGroupMetric(@PathVariable String groupId) {
        GroupMetricDTO metric = metricService.getGroupMetric(groupId);

        if (metric == null) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "그룹을 찾을 수 없습니다: " + groupId);
            return ResponseEntity.ok(error);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", metric);
        return ResponseEntity.ok(result);
    }

    /**
     * 모든 그룹의 메트릭 조회
     * GET /manage/inflow/group/metric
     */
    @GetMapping("/metric")
    public ResponseEntity<?> getAllGroupMetrics() {
        List<GroupMetricDTO> list = metricService.getAllGroupMetrics();

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", list);
        result.put("count", list.size());
        return ResponseEntity.ok(result);
    }

    /**
     * 특정 그룹의 메트릭 초기화
     * POST /manage/inflow/group/{groupId}/metric/reset
     */
    @PostMapping("/{groupId}/metric/reset")
    public ResponseEntity<?> resetGroupMetric(@PathVariable String groupId) {
        boolean ok = metricService.resetGroupMetric(groupId);

        Map<String, Object> result = new HashMap<>();
        result.put("success", ok);
        result.put("message", ok ? "초기화 완료: " + groupId : "그룹을 찾을 수 없습니다: " + groupId);
        return ResponseEntity.ok(result);
    }

    /**
     * 전체 그룹 메트릭 초기화
     * POST /manage/inflow/group/metric/reset
     */
    @PostMapping("/metric/reset")
    public ResponseEntity<?> resetAllGroupMetrics() {
        metricService.resetAllGroupMetrics();

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "전체 그룹 메트릭 초기화 완료");
        return ResponseEntity.ok(result);
    }
}
