package com.zioinfo.kintex.document;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.document.dto.DocumentReviewDto;
import com.zioinfo.kintex.document.dto.DocumentTransitionRequest;
import com.zioinfo.kintex.document.dto.MilestoneDto;
import com.zioinfo.kintex.document.dto.RequiredDocumentDto;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * M6 서류·마일스톤 API (SCR-22/23). 행사 RBAC 가드.
 * HWP/PDF 렌더(파일 큐)는 이번 스코프 제외 — 엔드포인트 미노출.
 */
@RestController
@RequestMapping("/api/events/{eventId}")
public class DocumentController {

    private final DocumentService service;
    private final EventAccessGuard guard;

    public DocumentController(DocumentService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** GET /milestones — 전시 마일스톤 진행 노드. */
    @GetMapping("/milestones")
    public ApiResponse<List<MilestoneDto>> milestones(@AuthenticationPrincipal KintexPrincipal principal,
                                                      @PathVariable String eventId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getMilestones(eventId));
    }

    /** GET /documents — 신고서류 체크리스트. */
    @GetMapping("/documents")
    public ApiResponse<List<RequiredDocumentDto>> documents(@AuthenticationPrincipal KintexPrincipal principal,
                                                            @PathVariable String eventId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getDocuments(eventId));
    }

    /** GET /documents/review — AI 서류 검수 + 전체 공정률. */
    @GetMapping("/documents/review")
    public ApiResponse<DocumentReviewDto> review(@AuthenticationPrincipal KintexPrincipal principal,
                                                 @PathVariable String eventId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getReview(eventId));
    }

    /** POST /documents/{docType} — 상태 전이(임시저장 save / 제출 submit). */
    @PostMapping("/documents/{docType}")
    public ApiResponse<RequiredDocumentDto> transition(@AuthenticationPrincipal KintexPrincipal principal,
                                                       @PathVariable String eventId,
                                                       @PathVariable String docType,
                                                       @RequestBody(required = false) DocumentTransitionRequest request) {
        guard.requireEventAccess(principal, eventId);
        String action = request == null ? null : request.action();
        return ApiResponse.ok(service.transition(eventId, docType, action));
    }
}
