package gc.mda.kcg.domain.fleet;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/vessel-analysis/groups")
@RequiredArgsConstructor
public class GroupPolygonController {

    private final GroupPolygonService groupPolygonService;

    /**
     * 전체 그룹 폴리곤 목록 (최신 스냅샷, 5분 캐시)
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getGroups() {
        List<GroupPolygonDto> groups = groupPolygonService.getLatestGroups();
        return ResponseEntity.ok(Map.of(
                "count", groups.size(),
                "items", groups
        ));
    }

    /**
     * 특정 그룹 상세 (멤버, 면적, 폴리곤 생성 근거)
     */
    @GetMapping("/{groupKey}/detail")
    public ResponseEntity<GroupPolygonDto> getGroupDetail(@PathVariable String groupKey) {
        GroupPolygonDto detail = groupPolygonService.getGroupDetail(groupKey);
        if (detail == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(detail);
    }

    /**
     * 특정 그룹 히스토리 (시간별 폴리곤 변화)
     */
    @GetMapping("/{groupKey}/history")
    public ResponseEntity<List<GroupPolygonDto>> getGroupHistory(
            @PathVariable String groupKey,
            @RequestParam(defaultValue = "24") int hours) {
        List<GroupPolygonDto> history = groupPolygonService.getGroupHistory(groupKey, hours);
        return ResponseEntity.ok(history);
    }
}
