package com.yaoai.api.controller;

import com.yaoai.api.dto.ai.StoryboardDTO;
import com.yaoai.common.response.ApiResponse;
import com.yaoai.domain.entity.Storyboard;
import com.yaoai.pipeline.service.StoryboardPipelineService;
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.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "分镜工作台")
@RestController
@RequestMapping("/projects/{projectId}")
@RequiredArgsConstructor
public class StoryboardController {

    private final StoryboardPipelineService storyboardService;

    @Operation(summary = "AI 批量生成分集分镜")
    @PostMapping("/episodes/{episodeId}/storyboards/generate")
    public ApiResponse<List<StoryboardDTO>> generate(@PathVariable Long projectId,
                                                      @PathVariable Long episodeId) {
        Long tenantId = TenantContext.get();
        return ApiResponse.success(
                storyboardService.generateStoryboards(episodeId, projectId, tenantId)
                        .stream().map(StoryboardDTO::from).collect(Collectors.toList())
        );
    }

    @Operation(summary = "获取分集分镜列表")
    @GetMapping("/episodes/{episodeId}/storyboards")
    public ApiResponse<List<StoryboardDTO>> listByEpisode(@PathVariable Long projectId,
                                                           @PathVariable Long episodeId) {
        Long tenantId = TenantContext.get();
        return ApiResponse.success(
                storyboardService.listByEpisode(episodeId, tenantId)
                        .stream().map(StoryboardDTO::from).collect(Collectors.toList())
        );
    }

    @Operation(summary = "获取项目全部分镜")
    @GetMapping("/storyboards")
    public ApiResponse<List<StoryboardDTO>> listByProject(@PathVariable Long projectId) {
        Long tenantId = TenantContext.get();
        return ApiResponse.success(
                storyboardService.listByProject(projectId, tenantId)
                        .stream().map(StoryboardDTO::from).collect(Collectors.toList())
        );
    }

    @Operation(summary = "新建单个分镜")
    @PostMapping("/episodes/{episodeId}/storyboards")
    public ApiResponse<StoryboardDTO> create(@PathVariable Long projectId,
                                              @PathVariable Long episodeId,
                                              @RequestBody Storyboard req) {
        Long tenantId = TenantContext.get();
        req.setProjectId(projectId);
        req.setEpisodeId(episodeId);
        req.setTenantId(tenantId);
        return ApiResponse.success(StoryboardDTO.from(storyboardService.create(req)));
    }

    @Operation(summary = "更新分镜")
    @PutMapping("/storyboards/{storyboardId}")
    public ApiResponse<StoryboardDTO> update(@PathVariable Long projectId,
                                              @PathVariable Long storyboardId,
                                              @RequestBody Storyboard req) {
        Long tenantId = TenantContext.get();
        return ApiResponse.success(StoryboardDTO.from(storyboardService.update(storyboardId, tenantId, req)));
    }

    @Operation(summary = "删除分镜")
    @DeleteMapping("/storyboards/{storyboardId}")
    public ApiResponse<Void> delete(@PathVariable Long projectId,
                                    @PathVariable Long storyboardId) {
        Long tenantId = TenantContext.get();
        storyboardService.delete(storyboardId, tenantId);
        return ApiResponse.success();
    }

    @Operation(summary = "批量排序（传入有序 ID 列表）")
    @PutMapping("/storyboards/reorder")
    public ApiResponse<Void> reorder(@PathVariable Long projectId,
                                     @RequestBody Map<String, List<Long>> body) {
        Long tenantId = TenantContext.get();
        storyboardService.reorder(body.get("ids"), tenantId);
        return ApiResponse.success();
    }

    @Operation(summary = "AI 生成分镜视频描述提示词")
    @PostMapping("/storyboards/{storyboardId}/generate-prompt")
    public ApiResponse<Map<String, String>> generatePrompt(@PathVariable Long projectId,
                                                           @PathVariable Long storyboardId) {
        Long tenantId = TenantContext.get();
        Map<String, String> body = Map.of("prompt", storyboardService.generatePrompt(storyboardId, tenantId));
        return ApiResponse.success(body);
    }
}
