package org.xyzh.usercenter.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.usercenter.collection.UserCollectionService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.dto.usercenter.TbUserCollection;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.system.utils.LoginUtil;
/**
 * @description 用户收藏控制器
 * @filename UserCollectionController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/usercenter/collections")
public class UserCollectionController {
    private static final Logger logger = LoggerFactory.getLogger(UserCollectionController.class);

    @Autowired
    private UserCollectionService userCollectionService;

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/user/{userID}")
    public ResultDomain<TbUserCollection> getUserCollections(
            @PathVariable("userID") String userID,
            @RequestParam(required = false) Integer resourceType,
            @RequestParam(required = false) String resourceID) {
        return userCollectionService.getUserCollections(userID, resourceType);
    }

    /**
     * 添加收藏
     */
    @PostMapping("/collect")
    public ResultDomain<TbUserCollection> addCollection(@RequestBody TbUserCollection userCollection) {
        return userCollectionService.addCollection(userCollection);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/collect")
    public ResultDomain<Boolean> removeCollection(@RequestBody TbUserCollection userCollection) {
        return userCollectionService.removeCollection(userCollection);
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    public ResultDomain<Boolean> isCollected(
            @RequestParam(value = "collectionID", required = false) String collectionID, 
            @RequestParam(value = "collectionType", required = false) Integer collectionType) {
        ResultDomain<Boolean> resultDomain = new ResultDomain<>();
        TbSysUser user = LoginUtil.getCurrentUser();
        if (user == null) {
            resultDomain.fail("请先登录");
            return resultDomain;
        }
        return userCollectionService.isCollected(user.getID(), collectionType, collectionID);
    }

    /**
     * 获取收藏数量
     */
    @GetMapping("/count/{userID}")
    public ResultDomain<Integer> getCollectionCount(
            @PathVariable("userID") String userID,
            @RequestParam(required = false) Integer resourceType,
            @RequestParam(required = false) String resourceID) {
        return userCollectionService.getCollectionCount(userID, resourceType, resourceID);
    }
}
