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

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.common.auth.UserContext;
import cn.reghao.tnb.content.api.dto.VideoCard;
import cn.reghao.tnb.content.app.vod.model.dto.UserRcmd;
import cn.reghao.tnb.content.app.vod.service.RecommendService;
import cn.reghao.tnb.user.api.iface.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2025-03-31 14:44:11
 */
@Tag(name = "推荐内容接口")
@RestController
@RequestMapping("/api/content/rcmd")
public class RecommendController {
    @DubboReference(check = false)
    private UserService userService;

    private final RecommendService recommendService;

    public RecommendController(RecommendService recommendService) {
        this.recommendService = recommendService;
    }

    @Operation(summary = "获取推荐视频", description = "N")
    @GetMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getRecommendVideos(@RequestParam(value = "nextId", required = false) String nextId) {
        if (Integer.parseInt(nextId) > 100) {
            return WebResult.success(Collections.emptyList());
        }

        long userId = UserContext.getUserId();
        List<VideoCard> list = recommendService.getRecommendVideos(userId, nextId);
        return WebResult.success(list);
    }

    @AuthUser
    @Operation(summary = "设置用户推荐模式", description = "N")
    @PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setUserRcmd(@RequestBody @Validated UserRcmd userRcmd) {
        Result result = recommendService.setRecommendMode(userRcmd.getMode());
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "获取用户推荐模式", description = "N")
    @GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUserRcmd() {
        boolean enableRcmd = recommendService.getRecommendMode();
        return WebResult.success(enableRcmd);
    }

    @AuthUser
    @Operation(summary = "设置用户不喜欢的视频", description = "N")
    @PostMapping(value = "/dislike/{videoId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String dislikeVideo(@PathVariable("videoId") String videoId) {
        long userId1 = UserContext.getUserId();
        recommendService.dislikeVideo(userId1, videoId);
        return WebResult.success();
    }
}
