package com.backendsys.controller.Ai;

import com.backendsys.aspect.HttpRequestAspect;
import com.backendsys.entity.Ai.Aiivh.AiivhMakeBroadcastAudioTask.AiivhMakeBroadcastAudioTaskDTO;
import com.backendsys.entity.PageDTO;
import com.backendsys.service.Ai.Aiivh.AiivhMakeBroadcastAudioTaskService;
import com.backendsys.utils.response.Result;
import com.backendsys.utils.response.ResultEnum;
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 AiivhMakeBroadcastAudioTaskController {

    @Autowired
    private HttpRequestAspect httpRequestAspect;

    @Autowired
    private AiivhMakeBroadcastAudioTaskService aiivhMakeBroadcastAudioTaskService;


    /**
     * 我的定制音频任务列表
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @GetMapping("/api/ai/ivh/getMyBroadcastAudio")
    public Result getPersonaudioListBySelf(@Validated PageDTO pageDTO, @Validated AiivhMakeBroadcastAudioTaskDTO aiivhMakeBroadcastAudioTaskDTO) {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastAudioTaskDTO.setUser_id(user_id);
        //
        return Result.success(aiivhMakeBroadcastAudioTaskService.queryAiivhMakeBroadcastAudioTask(pageDTO.getPage_num(), pageDTO.getPage_size(), aiivhMakeBroadcastAudioTaskDTO));
    }

    /**
     * 我的定制音频任务详情
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @GetMapping("/api/ai/ivh/getMyBroadcastAudioDetail")
    public Result getPersonaudioDetailBySelf(@Validated AiivhMakeBroadcastAudioTaskDTO aiivhMakeBroadcastAudioTaskDTO) throws IOException {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastAudioTaskDTO.setUser_id(user_id);
        //
        return Result.success(aiivhMakeBroadcastAudioTaskService.queryAiivhMakeBroadcastAudioTaskDetail(aiivhMakeBroadcastAudioTaskDTO));
    }

    /**
     * 创建定制音频播报任务
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @PostMapping("/api/ai/ivh/makeBroadcastAudio")
    public Result makePersonaudio(@Validated(AiivhMakeBroadcastAudioTaskDTO.Create.class) @RequestBody AiivhMakeBroadcastAudioTaskDTO aiivhMakeBroadcastAudioTaskDTO) throws IOException {

        if (aiivhMakeBroadcastAudioTaskDTO.getVirtualman_key().isEmpty() && aiivhMakeBroadcastAudioTaskDTO.getTimbre_key().isEmpty()) {
            return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), "形象或音色必须二选一");
        }

        return Result.success(aiivhMakeBroadcastAudioTaskService.insertAiivhMakeBroadcastAudioTask(aiivhMakeBroadcastAudioTaskDTO));
    }


    /**
     * 删除定制音频任务 (仅删除自己)
     */
    @PreAuthorize("@sr.hasPermission('33')")
    @DeleteMapping("/api/ai/ivh/deleteBroadcastAudio")
    public Result deletePersonaudio(@Validated @RequestBody AiivhMakeBroadcastAudioTaskDTO aiivhMakeBroadcastAudioTaskDTO) throws IOException {
        // [查询] 自身 UserId
        Long user_id = httpRequestAspect.getUserId();
        aiivhMakeBroadcastAudioTaskDTO.setUser_id(user_id);
        // 判断是否存在
        aiivhMakeBroadcastAudioTaskService.queryAiivhMakeBroadcastAudioTaskDetail(aiivhMakeBroadcastAudioTaskDTO);
        //
        return Result.success(aiivhMakeBroadcastAudioTaskService.deleteAiivhMakeBroadcastAudioTask(aiivhMakeBroadcastAudioTaskDTO));
    }

}
