package com.ycwl.basic.puzzle.edge.controller;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskFailRequest;
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeTaskSuccessRequest;
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeUploadUrlsResponse;
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncRequest;
import com.ycwl.basic.puzzle.edge.dto.PuzzleEdgeWorkerSyncResponse;
import com.ycwl.basic.puzzle.edge.task.PuzzleEdgeRenderTaskService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * Puzzle 边缘渲染对接接口
 * 模式：边缘客户端拉取任务（含上传地址） → 上传 → 上报结果
 */
@Slf4j
@IgnoreToken
@RestController
@RequestMapping("/puzzle/render/v1")
@RequiredArgsConstructor
public class PuzzleEdgeRenderTaskController {

    private final PuzzleEdgeRenderTaskService puzzleEdgeRenderTaskService;

    @PostMapping("/worker/sync")
    public ApiResponse<PuzzleEdgeWorkerSyncResponse> sync(@RequestBody PuzzleEdgeWorkerSyncRequest req) {
        return ApiResponse.success(puzzleEdgeRenderTaskService.sync(req));
    }

    @PostMapping("/task/{taskId}/uploadUrls")
    public ApiResponse<PuzzleEdgeUploadUrlsResponse> uploadUrls(@PathVariable Long taskId) {
        return ApiResponse.success(puzzleEdgeRenderTaskService.getUploadUrls(taskId));
    }

    @PostMapping("/task/{taskId}/success")
    public ApiResponse<String> success(@PathVariable Long taskId, @RequestBody PuzzleEdgeTaskSuccessRequest req) {
        puzzleEdgeRenderTaskService.taskSuccess(taskId, req);
        return ApiResponse.success("OK");
    }

    @PostMapping("/task/{taskId}/fail")
    public ApiResponse<String> fail(@PathVariable Long taskId, @RequestBody PuzzleEdgeTaskFailRequest req) {
        puzzleEdgeRenderTaskService.taskFail(taskId, req);
        return ApiResponse.success("OK");
    }
}
