package com.backendsys.controller.Ai;

import com.backendsys.aspect.HttpRequestAspect;
import com.backendsys.entity.Ai.Aiivh.AiivhMakeBroadcastTask.AiivhMakeBroadcastTaskCallbackDTO;
import com.backendsys.entity.Ai.Aiivh.AiivhMakeBroadcastTask.AiivhMakeBroadcastTaskDTO;
import com.backendsys.entity.PageDTO;
import com.backendsys.service.Ai.Aiivh.AiivhMakeBroadcastTaskService;
import com.backendsys.utils.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
public class AiivhMakeBroadcastTaskController {

    @Autowired
    private HttpRequestAspect httpRequestAspect;

    @Autowired
    private AiivhMakeBroadcastTaskService aiivhMakeBroadcastTaskService;


    /**
     * 我的定制视频任务列表
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @GetMapping("/api/ai/ivh/getMyBroadcast")
    public Result getPersonvideoListBySelf(@Validated PageDTO pageDTO, @Validated AiivhMakeBroadcastTaskDTO aiivhMakeBroadcastTaskDTO) {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastTaskDTO.setUser_id(user_id);
        //
        return Result.success(aiivhMakeBroadcastTaskService.queryAiivhMakeBroadcastTaskList(pageDTO.getPage_num(), pageDTO.getPage_size(), aiivhMakeBroadcastTaskDTO));
    }

    /**
     * 我的定制视频任务详情
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @GetMapping("/api/ai/ivh/getMyBroadcastDetail")
    public Result getPersonvideoDetailBySelf(@Validated AiivhMakeBroadcastTaskDTO aiivhMakeBroadcastTaskDTO) {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastTaskDTO.setUser_id(user_id);
        //
        return Result.success(aiivhMakeBroadcastTaskService.queryAiivhMakeBroadcastTaskDetail(aiivhMakeBroadcastTaskDTO));
    }

    /**
     * 创建定制视频播报任务
     * - 照片数智人不支持换背景
     *
     * (待做)
     * 开通的账号默认只分配了1路并发，只能同时提交一个制作任务。
     * 建议：
     * 1. 增购并发数
     * 2. 上游做排队逻辑 (待做)
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @PostMapping("/api/ai/ivh/makeBroadcast")
    public Result makePersonvideo(@Validated(AiivhMakeBroadcastTaskDTO.Create.class) @RequestBody AiivhMakeBroadcastTaskDTO aiivhMakeBroadcastTaskDTO) throws IOException {
        return Result.success(aiivhMakeBroadcastTaskService.insertAiivhMakeBroadcastTask(aiivhMakeBroadcastTaskDTO));
    }

    /**
     * [回调][公共接口] 当用户增加回调url时，将把视频制作结果以固定格式发送post请求到该url地址，固定格式见 附录二： 回调请求体格式。
     * 需注意：
     * 1. 限制CallbackUrl长度小于1000
     * 2. 只发送一次请求，无论是哪种问题导致的请求失败，都不会再进行发送。
     *
     * 附录二： 回调请求体格式
     * https://cloud.tencent.com/document/product/1240/81279
     */
    @PostMapping("/api/public/ai/ivh/makeBroadcastCallback")
    public Result makePersonvideoCallback(@Validated @RequestBody AiivhMakeBroadcastTaskCallbackDTO aiivhMakeBroadcastTaskCallbackDTO) {
        System.out.println("---------------------------------------");
        System.out.println("makePersonvideoCallback: ");
        System.out.println(aiivhMakeBroadcastTaskCallbackDTO);
        System.out.println("---------------------------------------");
        return Result.success(aiivhMakeBroadcastTaskService.updateAiivhMakeBroadcastTask(aiivhMakeBroadcastTaskCallbackDTO));
    }

    /**
     * 删除定制视频任务 (仅删除自己)
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @DeleteMapping("/api/ai/ivh/deleteBroadcast")
    public Result deletePersonvideo(@Validated @RequestBody AiivhMakeBroadcastTaskDTO aiivhMakeBroadcastTaskDTO) {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastTaskDTO.setUser_id(user_id);
        // 判断是否存在
        aiivhMakeBroadcastTaskService.queryAiivhMakeBroadcastTaskDetail(aiivhMakeBroadcastTaskDTO);
        //
        return Result.success(aiivhMakeBroadcastTaskService.deleteAiivhMakeBroadcastTask(aiivhMakeBroadcastTaskDTO));
    }

}
