package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.admin.request.TeachTextAudiosQueryReq;
import com.seer.teach.admin.request.TeachTextAudiosReq;
import com.seer.teach.admin.response.TeachTextAudiosResp;
import com.seer.teach.admin.service.AdminTeachTextAudiosService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.teacher.service.platform.StreamingTextToAudioResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 管理端 - 讲课稿音频和字幕管理
 * </p>
 *
 * @author Lingma
 * @since 2025-12-12
 */
@RestController
@RequestMapping("/teacher/teach-text-audio")
@RequiredArgsConstructor
@Tag(name = "管理端 - 讲课稿音频和字幕管理")
@Validated
public class AdminTeachTextAudiosController {

    private final AdminTeachTextAudiosService adminTeachTextAudiosService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询讲课稿音频和字幕列表")
    @SaCheckPermission("teacher:admin:teach-text-audios:list")
    public ResultBean<PageListBean<TeachTextAudiosResp>> pageTeachTextAudios(@RequestBody @Validated TeachTextAudiosQueryReq req) {
        IPage<TeachTextAudiosResp> page = adminTeachTextAudiosService.pageTeachTextAudios(req);
        return ResultBean.success(new PageListBean<>(page));
    }
    
    @GetMapping(value = "/stream-play", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "流式播放课稿音频")
    @SaCheckPermission("teacher:admin:teach-text-audios:stream-play")
    public Flux<ServerSentEvent<StreamingTextToAudioResult>> streamPlayTeachTextAudio(@RequestParam("textId") Integer textId) {
        return adminTeachTextAudiosService.streamPlayTeachTextAudioSse(textId);
    }

    @PostMapping("/generated")
    @Operation(summary = "生成讲课稿音频和字幕")
    @SaCheckPermission("teacher:admin:teach-text-audios:generated")
    public ResultBean<Boolean> generatedTeachTextAudio(@Parameter(description = "生成讲课稿音频和字幕") @Valid @RequestBody TeachTextAudiosReq req) {
        boolean result = adminTeachTextAudiosService.generatedTeachTextAudio(req);
        return ResultBean.success(result);
    }

    @DeleteMapping("/{textId}")
    @Operation(summary = "删除讲课稿音频和字幕")
    @SaCheckPermission("teacher:admin:teach-text-audios:delete")
    public ResultBean<Boolean> deleteTeachTextAudioByTextId(@Parameter(description = "音频和字幕ID") @PathVariable @NotNull(message = "讲课稿ID不能为空") Integer textId) {
        return ResultBean.success(adminTeachTextAudiosService.deleteTeachTextAudioByTextId(textId));
    }
}