package com.zioinfo.kintex.module.m5;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.module.m5.dto.RenderJobDto;
import com.zioinfo.kintex.module.m5.dto.RenderJobRequest;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * M5 나노바나나 시각화 — RenderJob 발행/상태 API (SCR-06/12). 행사 RBAC 가드.
 * 발행은 큐 적재까지만(비동기) — 완료는 워커 콜백 → WebSocket /topic/render/{jobId} 푸시.
 */
@RestController
@RequestMapping("/api/events/{eventId}")
public class RenderJobController {

    private final RenderJobService service;
    private final EventAccessGuard guard;

    public RenderJobController(RenderJobService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** POST /booths/{boothId}/render — RenderJob 발행(행사 멤버). 쿼터 초과 시 429. */
    @PostMapping("/booths/{boothId}/render")
    public ApiResponse<RenderJobDto> publish(@AuthenticationPrincipal KintexPrincipal principal,
                                             @PathVariable String eventId,
                                             @PathVariable String boothId,
                                             @Valid @RequestBody RenderJobRequest request) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.publish(eventId, boothId, request));
    }

    /** GET /render-jobs/{jobId} — 잡 상태 조회. */
    @GetMapping("/render-jobs/{jobId}")
    public ApiResponse<RenderJobDto> status(@AuthenticationPrincipal KintexPrincipal principal,
                                            @PathVariable String eventId,
                                            @PathVariable String jobId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getStatus(eventId, jobId));
    }

    /** GET /booths/{boothId}/render-jobs — 부스 갤러리 목록(SCR-12). */
    @GetMapping("/booths/{boothId}/render-jobs")
    public ApiResponse<List<RenderJobDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                                @PathVariable String eventId,
                                                @PathVariable String boothId,
                                                @RequestParam(required = false) String shot) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.listByBooth(boothId, shot));
    }
}
