package com.hotwj.platform.video.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hotwj.platform.video.domain.bo.VideoChunkUploadBo;
import com.hotwj.platform.video.domain.bo.VideoMergeBo;
import com.hotwj.platform.video.domain.vo.VideoTranscodeTaskInfo;
import com.hotwj.platform.video.service.VideoChunkUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 视频分片上传与转码接口
 */
@SaIgnore
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "视频分片上传", description = "分片上传、合并和转码任务")
public class VideoChunkUploadController {

    private final VideoChunkUploadService videoChunkUploadService;

    @GetMapping("/api/check")
    @Operation(summary = "检查视频分片上传状态")
    public R<List<Integer>> check(@RequestParam String fileHash) {
        return R.ok(videoChunkUploadService.checkUploadedChunks(fileHash));
    }

    @PostMapping(value = "/api/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传视频分片")
    public R<Void> upload(@Valid VideoChunkUploadBo bo) {
        videoChunkUploadService.uploadChunk(bo);
        return R.ok();
    }

    @PostMapping("/api/merge")
    @Operation(summary = "合并视频分片并创建转码任务")
    public R<Map<String, String>> merge(@Valid @RequestBody VideoMergeBo bo) {
        return R.ok(videoChunkUploadService.mergeAndCreateTask(bo));
    }

    @GetMapping("/api/task/{taskId}")
    @Operation(summary = "查询视频转码任务状态")
    public R<VideoTranscodeTaskInfo> task(@PathVariable String taskId) {
        return R.ok(videoChunkUploadService.getTaskInfo(taskId));
    }
}
