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

import cn.reghao.jutil.web.WebResult;
import cn.reghao.tnb.content.api.dto.spider.BiliRegion;
import cn.reghao.tnb.content.api.dto.comment.CommentSbtDto;
import cn.reghao.tnb.content.api.dto.VideoFileUpdate;
import cn.reghao.tnb.content.api.dto.spider.CrawledVideo;
import cn.reghao.tnb.content.app.vod.service.CrawledDataService;
import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
import cn.reghao.tnb.content.app.vod.service.CommentService;
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-11-25 14:50:15
 */
@Tag(name = "爬取数据提交接口")
@RestController
@RequestMapping("/api/content/post/video")
public class CrawledDataController {
    private final CrawledDataService crawledDataService;
    private final CommentService commentService;

    public CrawledDataController(CrawledDataService crawledDataService, CommentService commentService) {
        this.crawledDataService = crawledDataService;
        this.commentService = commentService;
    }

    @Operation(summary = "添加 bili 视频稿件", description = "N")
    @PostMapping("/bili")
    public String addBiliVideoPost(@RequestBody @Validated CrawledVideo crawledVideo) {
        crawledDataService.publishVideo(crawledVideo);
        return WebResult.success();
    }

    @Operation(summary = "添加 bili 视频评论", description = "N")
    @PostMapping("/bili/comment")
    public String addBiliVideoComment(@RequestBody CommentSbtDto commentSbtDto) {
        commentService.saveBiliComment(commentSbtDto);
        return WebResult.success();
    }

    @Operation(summary = "添加 bili 视频数据", description = "N")
    @PostMapping("/bili/statistic")
    public String addBiliVideoStatistic(@RequestBody @Validated CrawledVideo crawledVideo) {
        crawledDataService.updateVideoStatistic(crawledVideo);
        return WebResult.success();
    }

    @Operation(summary = "添加 bili 视频资源", description = "N")
    @PostMapping("/bili/update")
    public String updateVideoFile(@RequestBody @Validated VideoFileUpdate videoFileUpdate) {
        crawledDataService.updateVideoFile(videoFileUpdate);
        return WebResult.success();
    }

    @Operation(summary = "查询 bili 视频稿件", description = "N")
    @GetMapping("/bili")
    public String getBiliVideoPost(@RequestParam("videoId") String videoId) {
        VideoPost videoPost = crawledDataService.videoExist(videoId);
        if (videoPost != null) {
            return WebResult.success(true);
        }

        return WebResult.success(false);
    }

    @Operation(summary = "获取 bili 视频分区", description = "N")
    @GetMapping(value = "/categories/bili", produces = MediaType.APPLICATION_JSON_VALUE)
    public String videoCategories() {
        List<BiliRegion> list = crawledDataService.getCategories();
        return WebResult.success(list);
    }
}
