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.Operation;
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.redis.redis.RedisCache;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;


@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;

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

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

    @Operation(summary = "视频详情")
    @GetMapping("/detail/{vlogId}")
    public R<Object> detail(@PathVariable String vlogId) {

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


    @Operation(summary = "我的私密视频列表")
    @PostMapping("/myPrivateList")
    public R<Page<IndexVlogVO>> myPrivateList(@RequestBody MyListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
           return R.notLogin();
        }
        bo.setUserId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.queryMyVlogList(bo, page);
        return R.ok(pages);
    }

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

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

    @Operation(summary = "我关注人的视频列表")
    @PostMapping("/followList")
    public R<Page<IndexVlogVO>> followList(@RequestBody SimpleListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.notLogin();
        }
        bo.setMyId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.getMyFollowVlogList(page);
        return R.ok(pages);
    }

    @Operation(summary = "好友视频列表")
    @PostMapping("/friendList")
    public R<Page<IndexVlogVO>> friendList(@RequestBody SimpleListBO bo, @RequestBody Page page) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.notLogin();
        }
        bo.setMyId(String.valueOf(loginUser.getUserId()));
        Page<IndexVlogVO> pages = vlogService.getMyFriendVlogList(page);
        return R.ok(pages);
    }


    @Operation(summary = "上传视频到云点播")
    @PostMapping("publish")
    public R<Void> publish(@RequestBody VlogBO vlogBO) throws Exception {
        vlogService.createVlog(vlogBO);
        return R.ok();
    }

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

    @Operation(summary = "点赞")
    @PostMapping("/like")
    public R<Void> like(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.notLogin();
        }
        String userId = String.valueOf(loginUser.getUserId());
        String vlogId = vlogBO.getId();

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



    @Operation(summary = "取消点赞")
    @PostMapping("/unlike")
    public R<Void> unlike(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.notLogin();
        }
        String userId = String.valueOf(loginUser.getUserId());
        String vlogId = vlogBO.getId();
        vlogService.userUnLikeVlog(userId, vlogId);
        return R.ok();
    }

    @Operation(summary = "已读视频")
    @GetMapping("/read")
    public R<Void> read(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.notLogin();
        }
          vlogService.readVlog(loginUser.getUserId(), vlogBO.getId());
        return R.ok();
    }
    @Operation(summary = "删除视频")
    @DeleteMapping("/delete/{id}")
    public R<Void> delete(@PathVariable String id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        vlogService.removeById(id);
        return R.ok();
    }

    @Operation(summary = "视频列表")
    @PostMapping("/page")
    public R<Page<IndexVlogVO>> page(@RequestBody VlogBO vlogBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        if(vlogBO.getMemberId()==null){
            throw new ServiceException("用户id不能为空");
        }
        return R.ok(vlogService.getIndexVlogList(vlogBO,vlogBO.getPage()));
    }


    @Operation(summary = "手动触发缓存点赞最多视频")
    @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());
        }
    }
}
