package com.yaoai.api.controller;

import com.yaoai.api.dto.ai.AssemblyTaskDTO;
import com.yaoai.common.response.ApiResponse;
import com.yaoai.domain.entity.AssemblyTask;
import com.yaoai.pipeline.service.AssemblyPipelineService;
import com.yaoai.security.context.TenantContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "视频合成")
@RestController
@RequestMapping("/projects/{projectId}/episodes/{episodeId}/assembly")
@RequiredArgsConstructor
public class AssemblyController {

    private final AssemblyPipelineService assemblyPipelineService;

    @Operation(summary = "启动合成任务")
    @PostMapping
    public ApiResponse<AssemblyTaskDTO> start(@PathVariable Long projectId,
                                               @PathVariable Long episodeId) {
        Long tenantId = TenantContext.get();
        AssemblyTask task = assemblyPipelineService.startAssembly(projectId, episodeId, tenantId);
        return ApiResponse.success(AssemblyTaskDTO.from(task, null));
    }

    @Operation(summary = "查询合成任务状态")
    @GetMapping
    public ApiResponse<AssemblyTaskDTO> get(@PathVariable Long projectId,
                                             @PathVariable Long episodeId) {
        Long tenantId = TenantContext.get();
        AssemblyTask task = assemblyPipelineService.getLatest(episodeId, tenantId);
        if (task == null) {
            return ApiResponse.success(null);
        }
        String downloadUrl = assemblyPipelineService.signedDownloadUrl(task);
        return ApiResponse.success(AssemblyTaskDTO.from(task, downloadUrl));
    }
}
