package com.zioinfo.kintex.module.m5.reroom;

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.reroom.dto.ReRoomRenderRequest;
import com.zioinfo.kintex.module.m5.reroom.dto.ReferenceImageDto;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * M5 ReRoomAI 방식 사진 시안 — 참조 이미지 업로드 + image-to-image 렌더 발행(SCR-06 "사진 시안" 섹션).
 * 소유자 지시 13.2. 행사 RBAC 가드. 상태 조회/콜백/푸시는 기존 RenderJob 경로 재사용
 * (GET /api/events/{eventId}/render-jobs/{jobId}).
 */
@RestController
@RequestMapping("/api/events/{eventId}/booths/{boothId}/reroom")
public class ReRoomStudioController {

    private final ReRoomStudioService service;
    private final EventAccessGuard guard;

    public ReRoomStudioController(ReRoomStudioService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** POST /reference — 빈 부스/공간 참조 이미지 업로드(multipart {@code file}) → {referenceId, url}. */
    @PostMapping(value = "/reference", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResponse<ReferenceImageDto> uploadReference(@AuthenticationPrincipal KintexPrincipal principal,
                                                          @PathVariable String eventId,
                                                          @PathVariable String boothId,
                                                          @RequestPart("file") MultipartFile file) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.uploadReference(file));
    }

    /** POST /render — 사진 시안 렌더 발행(참조ID·스타일·자연어 지시) → RenderJobDto(QUEUED). */
    @PostMapping("/render")
    public ApiResponse<RenderJobDto> render(@AuthenticationPrincipal KintexPrincipal principal,
                                            @PathVariable String eventId,
                                            @PathVariable String boothId,
                                            @Valid @RequestBody ReRoomRenderRequest request) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.render(eventId, boothId, request));
    }
}
