package gc.mda.kcg.domain.fleet;

import gc.mda.kcg.domain.fleet.dto.*;
import gc.mda.kcg.permission.annotation.RequirePermission;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/parent-inference")
@RequiredArgsConstructor
public class ParentInferenceWorkflowController {

    private final ParentInferenceWorkflowService service;

    // ========================================================================
    // 검토 대기 / 결과 조회
    // ========================================================================

    @GetMapping("/review")
    @RequirePermission(resource = "parent-inference-workflow:parent-review", operation = "READ")
    public Page<ParentResolution> listReview(
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return service.listReview(status, PageRequest.of(page, size));
    }

    // ========================================================================
    // 모선 확정/거부/리셋
    // ========================================================================

    @PostMapping("/groups/{groupKey}/{subClusterId}/review")
    @RequirePermission(resource = "parent-inference-workflow:parent-review", operation = "UPDATE")
    public ParentResolution review(
            @PathVariable String groupKey,
            @PathVariable Integer subClusterId,
            @Valid @RequestBody ReviewRequest req
    ) {
        return service.review(groupKey, subClusterId, req);
    }

    // ========================================================================
    // 후보 제외 (그룹 / 전역)
    // ========================================================================

    @PostMapping("/groups/{groupKey}/{subClusterId}/exclusions")
    @RequirePermission(resource = "parent-inference-workflow:parent-exclusion", operation = "CREATE")
    public CandidateExclusion excludeForGroup(
            @PathVariable String groupKey,
            @PathVariable Integer subClusterId,
            @Valid @RequestBody ExclusionRequest req
    ) {
        return service.excludeForGroup(groupKey, subClusterId, req);
    }

    @PostMapping("/exclusions/global")
    @RequirePermission(resource = "parent-inference-workflow:exclusion-management", operation = "CREATE")
    public CandidateExclusion excludeGlobal(@Valid @RequestBody GlobalExclusionRequest req) {
        return service.excludeGlobal(req);
    }

    @PostMapping("/exclusions/{exclusionId}/release")
    @RequirePermission(resource = "parent-inference-workflow:parent-exclusion", operation = "DELETE")
    public CandidateExclusion releaseExclusion(
            @PathVariable Long exclusionId,
            @RequestBody(required = false) CancelRequest req
    ) {
        return service.releaseExclusion(exclusionId, req);
    }

    @GetMapping("/exclusions")
    @RequirePermission(resource = "parent-inference-workflow:parent-exclusion", operation = "READ")
    public Page<CandidateExclusion> listExclusions(
            @RequestParam(required = false) String scopeType,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return service.listExclusions(scopeType, PageRequest.of(page, size));
    }

    // ========================================================================
    // 학습 세션
    // ========================================================================

    @PostMapping("/groups/{groupKey}/{subClusterId}/label-sessions")
    @RequirePermission(resource = "parent-inference-workflow:label-session", operation = "CREATE")
    public LabelSession createLabelSession(
            @PathVariable String groupKey,
            @PathVariable Integer subClusterId,
            @Valid @RequestBody LabelSessionRequest req
    ) {
        return service.createLabelSession(groupKey, subClusterId, req);
    }

    @PostMapping("/label-sessions/{sessionId}/cancel")
    @RequirePermission(resource = "parent-inference-workflow:label-session", operation = "UPDATE")
    public LabelSession cancelLabelSession(
            @PathVariable Long sessionId,
            @RequestBody(required = false) CancelRequest req
    ) {
        return service.cancelLabelSession(sessionId, req);
    }

    @GetMapping("/label-sessions")
    @RequirePermission(resource = "parent-inference-workflow:label-session", operation = "READ")
    public Page<LabelSession> listLabelSessions(
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return service.listLabelSessions(status, PageRequest.of(page, size));
    }

    // ========================================================================
    // 도메인 로그 (운영자 액션 이력)
    // ========================================================================

    @GetMapping("/review-logs")
    @RequirePermission(resource = "parent-inference-workflow:parent-review", operation = "READ")
    public Page<ParentReviewLog> listReviewLogs(
            @RequestParam(required = false) String groupKey,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size
    ) {
        return service.listReviewLogs(groupKey, PageRequest.of(page, size));
    }
}
