package org.dromara.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.domain.bo.*;
import com.wzj.soopin.content.domain.vo.IndexVlogVO;
import com.wzj.soopin.content.service.IVlogPullService;
import com.wzj.soopin.content.service.VlogService;
import com.wzj.soopin.content.service.VlogUploadService;
import com.wzj.soopin.content.utils.QcCloud;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mq.domain.MQMessage;
import org.dromara.common.redis.redis.RedisCache;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@Slf4j
@Api(tags = "VlogController 短视频相关业务功能的接口")
@RequestMapping("/app/vlog")
@RestController
@AllArgsConstructor
public class AppVlogController {
    private final VlogService vlogService;
    private final QcCloud qcCloud;
    private final VlogUploadService vlogUploadService;
    public final RedisCache cache;

    private final IVlogPullService pullService;

    @Tag(name = "首页视频列表")
    @PostMapping("/indexList")
    public R<Page<IndexVlogVO>> indexList(@RequestBody IndexListBO bo, @RequestBody Page page) {

        Page<IndexVlogVO> pages = pullService.pullFromMq(page);
        return R.ok(pages);
    }

    @GetMapping("/detail/{vlogId}")
    public R<Object> detail(@PathVariable String vlogId) {

        return R.ok(vlogService.getVlogDetailById(vlogId));
    }


    @Tag(name = "我的私密视频列表")
    @PostMapping("/myPrivateList")
    public R<Page<IndexVlogVO>> myPrivateList(@RequestBody MyListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        bo.setUserId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.queryMyVlogList(bo, page);
        return R.ok(pages);
    }

    @Tag(name = "我点赞的视频列表")
    @PostMapping("/myLikedList")
    public R<Page<IndexVlogVO>> myLikedList(@RequestBody MyLikedVlogBO bo, @RequestBody Page page) {

        Page<IndexVlogVO> pages = vlogService.getMyLikedVlogList(page);
        return R.ok(pages);
    }

    @Tag(name = "我关注的人的视频列表")
    @PostMapping("/followList")
    public R<Page<IndexVlogVO>> followList(@RequestBody SimpleListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        bo.setMyId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.getMyFollowVlogList(page);
        return R.ok(pages);
    }

    @Tag(name = "好友视频列表")
    @PostMapping("/friendList")
    public R<Page<IndexVlogVO>> friendList(@RequestBody SimpleListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        bo.setMyId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.getMyFriendVlogList(page);
        return R.ok(pages);
    }

    /**
     * 上传视频到云点播
     * @param vlogBO
     * @return
     * @throws Exception
     */
    @PostMapping("publish")
    public R<Void> publish(@RequestBody VlogBO vlogBO) throws Exception {



        vlogService.createVlog(vlogBO);



        return R.ok();
    }

    @Tag(name = "我的公开视频列表")
    @PostMapping("/myPublicList")
    public R<Page<IndexVlogVO>> myPublicList(@RequestBody MyListBO bo, @RequestBody Page page) {
        Page<IndexVlogVO> pages = vlogService.queryMyVlogList(bo, page);
        return R.ok(pages);
    }

    @PostMapping("/like")
    public R<Void> like(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        String userId = String.valueOf(loginUser.getUserId());
        String vlogId = vlogBO.getId();

        vlogService.userLikeVlog(userId,vlogId );
        return R.ok();
    }



    @PostMapping("/unlike")
    public R<Void> unlike(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        String userId = String.valueOf(loginUser.getUserId());
        String vlogId = vlogBO.getId();
        vlogService.userUnLikeVlog(userId, vlogId);
        return R.ok();
    }
    @GetMapping("/read")
    public R<Void> read(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
          vlogService.readVlog(loginUser.getUserId(), vlogBO.getId());
        return R.ok();
    }

    @Tag(name = "手动触发缓存点赞最多视频")
    @PostMapping("/cacheTopLikedVlogs")
    public R<Void> cacheTopLikedVlogs(@RequestParam(defaultValue = "100") int limit) {
        try {
//            vlogService.cacheTopLikedVlogs(limit);
            return R.ok();
        } catch (Exception e) {
            log.error("手动触发缓存点赞最多视频失败", e);
            return R.fail("缓存失败: " + e.getMessage());
        }
    }
}
