package cn.iocoder.byzs.module.ai.controller.admin.tts;

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.byzs.framework.common.enums.CommonStatusEnum;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;
import cn.iocoder.byzs.module.ai.controller.admin.model.vo.tool.AiToolRespVO;
import cn.iocoder.byzs.module.ai.controller.admin.tts.vo.AiTtsPageReqVO;
import cn.iocoder.byzs.module.ai.controller.admin.tts.vo.AiTtsRespVO;
import cn.iocoder.byzs.module.ai.controller.admin.tts.vo.AiTtsSaveReqVO;
import cn.iocoder.byzs.module.ai.dal.dataobject.tts.AiTtsDO;
import cn.iocoder.byzs.module.ai.service.tts.AiTtsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - AI TTS文转音")
@RestController
@RequestMapping("/ai/tts")
@Validated
@Slf4j
public class AiTtsController {

    @Resource
    private AiTtsService ttsService;

    @PostMapping("/create")
    @Operation(summary = "创建AI TTS文转音")
    @PreAuthorize("@ss.hasPermission('ai:tts:create')")
    public CommonResult<Long> createTts(@Valid @RequestBody AiTtsSaveReqVO createReqVO) {
        return success(ttsService.createTts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI TTS文转音")
    @PreAuthorize("@ss.hasPermission('ai:tts:update')")
    public CommonResult<Boolean> updateTts(@Valid @RequestBody AiTtsSaveReqVO updateReqVO) {
        ttsService.updateTts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI TTS文转音")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:tts:delete')")
    public CommonResult<Boolean> deleteTts(@RequestParam("id") Long id) {
        ttsService.deleteTts(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI TTS文转音")
                @PreAuthorize("@ss.hasPermission('ai:tts:delete')")
    public CommonResult<Boolean> deleteTtsList(@RequestParam("ids") List<Long> ids) {
        ttsService.deleteTtsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI TTS文转音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:tts:query')")
    public CommonResult<AiTtsRespVO> getTts(@RequestParam("id") Long id) {
        AiTtsDO tts = ttsService.getTts(id);
        return success(BeanUtils.toBean(tts, AiTtsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI TTS文转音分页")
    @PreAuthorize("@ss.hasPermission('ai:tts:query')")
    public CommonResult<PageResult<AiTtsRespVO>> getTtsPage(@Valid AiTtsPageReqVO pageReqVO) {
        PageResult<AiTtsDO> pageResult = ttsService.getTtsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiTtsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI TTS文转音 Excel")
    @PreAuthorize("@ss.hasPermission('ai:tts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTtsExcel(@Valid AiTtsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiTtsDO> list = ttsService.getTtsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI TTS文转音.xls", "数据", AiTtsRespVO.class,
                        BeanUtils.toBean(list, AiTtsRespVO.class));
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得tts的精简列表")
    public CommonResult<List<AiToolRespVO>> getTtsSimpleList() {
        List<AiTtsDO> list = ttsService.getTtsSimpleListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, tool -> new AiToolRespVO()
                .setId(tool.getId()).setName(tool.getName())));
    }

    @PostMapping("/convert")
    @Operation(summary = "文本转语音")
    @PreAuthorize("@ss.hasPermission('ai:tts:convert')")
    public CommonResult<String> convertTextToSpeech(@RequestParam("roleId") Long roleId, @RequestParam("content") String content) {
        // 执行文本转语音
        String audioUrl = ttsService.convertTextToSpeech(roleId, content);
        // 返回结果
        return success(audioUrl);
    }

}