package cn.reghao.tnb.content.app.vod.controller;

import cn.reghao.file.api.dto.VideoMeta;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.content.api.dto.VideoFilePublish;
import cn.reghao.tnb.content.api.dto.VideoFileUpdate;
import cn.reghao.tnb.content.api.dto.VideoPublishSbt;
import cn.reghao.tnb.content.app.vod.model.dto.*;
import cn.reghao.tnb.content.api.dto.UserVideoPost;
import cn.reghao.tnb.content.app.vod.service.VideoPostQuery;
import cn.reghao.tnb.content.app.vod.service.VideoPostService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-10-08 15:34:48
 */
@Tag(name = "视频稿件接口")
@RestController
@RequestMapping("/api/content/post/video")
public class VideoPostController {
    private final VideoPostService videoPostService;
    private final VideoPostQuery videoPostQuery;

    public VideoPostController(VideoPostService videoPostService, VideoPostQuery videoPostQuery) {
        this.videoPostService = videoPostService;
        this.videoPostQuery = videoPostQuery;
    }

    @AuthUser
    @Operation(summary = "发布视频文件", description = "N")
    @PostMapping("/publish/file")
    public String addVideoFile(@RequestBody @Validated VideoFilePublish videoFilePublish) throws Exception {
        String videoId = videoPostService.publishVideoFile(videoFilePublish);
        return WebResult.success(videoId);
    }

    @AuthUser
    @Operation(summary = "发布视频稿件", description = "N")
    @PostMapping("/publish/post")
    public String publishVideoPost(@RequestBody @Validated VideoPublishSbt videoPublishSbt) {
        Result result = videoPostService.publishVideoPost(videoPublishSbt);
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "修改视频可见范围", description = "N")
    @PostMapping("/update/scope")
    public String updateVideoScope(@RequestBody @Validated VideoScopeUpdate videoScopeUpdate) {
        videoPostService.updateVideoScope(videoScopeUpdate);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "修改视频稿件信息", description = "N")
    @PostMapping("/update/info")
    public String updateVideoPost(@RequestBody @Validated VideoInfoUpdate videoInfoUpdate) {
        videoPostService.updateVideoInfo(videoInfoUpdate);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "修改视频稿件封面", description = "N")
    @PostMapping("/update/cover")
    public String updateVideoCover(@RequestBody @Validated VideoCoverUpdate videoCoverUpdate) {
        videoPostService.updateVideoCover(videoCoverUpdate);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "修改视频稿件文件", description = "N")
    @PostMapping("/update/file")
    public String updateVideoFile(@RequestBody @Validated VideoFileUpdate videoFileUpdate) {
        videoPostService.updateVideoFile(videoFileUpdate);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "删除视频稿件", description = "N")
    @DeleteMapping("/{videoId}")
    public String deleteVideoPost(@PathVariable("videoId") String videoId) {
        videoPostService.deleteVideo(videoId);
        return WebResult.success();
    }
    
    @Operation(summary = "获取视频稿件列表", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoPosts(@RequestParam("page") int page) {
        PageList<UserVideoPost> pageList = videoPostQuery.getUserVideoPost(page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取视频稿件详情", description = "N")
    @GetMapping(value = "/{videoId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoPost(@PathVariable("videoId") String videoId) {
        UserVideoPost userVideoPost = videoPostQuery.getUserVideoDetail(videoId);
        return WebResult.success(userVideoPost);
    }

    @AuthUser
    @Operation(summary = "获取视频稿件的文件资源", description = "N")
    @GetMapping("/resource/{videoId}")
    public String getVideoResource(@PathVariable("videoId") String videoId) {
        List<VideoMeta> list = videoPostQuery.getVideoResource(videoId);
        return WebResult.success(list);
    }
}
