package com.backendsys.modules.ai.volcengine.controller;

import com.backendsys.modules.ai.volcengine.entity.*;
import com.backendsys.modules.ai.volcengine.service.VolcengineVideoCollectService;
import com.backendsys.modules.ai.volcengine.service.VolcengineVideoTaskService;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@AllArgsConstructor
@Validated
@RestController
@Tag(name = "火山大模型 视频生成")
public class VolcengineVideoController {

    private final VolcengineVideoTaskService volcengineVideoTaskService;

    private final VolcengineVideoCollectService volcengineVideoCollectService;


    @Operation(summary = "创建图生视频任务")
    @PreAuthorize("@sr.hasPermission('37.1.1')")
    @PostMapping("/api/ai/volcengine/generateVideo")
    public Result generateVideo(@Validated(VolcengineVideoTask.Generate.class) @RequestBody VolcengineVideoTask volcengineVideoTask) {
        volcengineVideoTask.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", volcengineVideoTaskService.generateVideo(volcengineVideoTask));
    }

    @Operation(summary = "查询图生视频任务状态")
    @PreAuthorize("@sr.hasPermission('37.1.2')")
    @GetMapping("/api/ai/volcengine/video/getTaskStatus/{volcengineTaskId}")
    public Result getTaskStatus(@PathVariable String volcengineTaskId) {
        return Result.success().put("data", volcengineVideoTaskService.selectTaskStatus(volcengineTaskId));
    }


    @Operation(summary = "分页获取图生视频任务记录")
    @PreAuthorize("@sr.hasPermission('37.1.3')")
    @GetMapping("/api/ai/volcengine/video/getTaskList")
    public Result getTaskList(VolcengineVideoTaskDTO dto) {
        dto.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", volcengineVideoTaskService.selectTaskList(dto));
    }

    @Operation(summary = "视频收藏/取消收藏")
    @PreAuthorize("@sr.hasPermission('37.1.4')")
    @PutMapping("/api/ai/volcengine/video/setCollect")
    public Result setCollect(@Validated(VolcengineVideoCollect.Collect.class) @RequestBody VolcengineVideoCollect volcengineVideoCollect) {
        volcengineVideoCollect.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", volcengineVideoCollectService.setCollect(volcengineVideoCollect));
    }

    @Operation(summary = "重新生成视频")
    @PreAuthorize("@sr.hasPermission('37.1.5')")
    @PostMapping("/api/ai/volcengine/video/regenerate")
    public Result regenerate(@Validated(VolcengineVideoTaskDetail.Regenerate.class) @RequestBody VolcengineVideoTaskDetail volcengineVideoTaskDetail) {
        volcengineVideoTaskDetail.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", volcengineVideoTaskService.regenerate(volcengineVideoTaskDetail));
    }

    @Anonymous
    @Operation(summary = "图生视频状态通知")
    @PostMapping("/api/ai/volcengine/video/notify")
    public Result notify(@Validated @RequestBody VolcengineVideoTaskNotify volcengineVideoTaskNotify) {
        volcengineVideoTaskService.notify(volcengineVideoTaskNotify);
        return Result.success();
    }

}
