package org.xyzh.news.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.news.recommend.ResourceRecommendService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.resource.TbResourceRecommend;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.vo.ResourceRecommendVO;
import org.xyzh.system.utils.LoginUtil;

import java.util.List;
import java.util.Map;

/**
 * @description 资源推荐控制器
 * @filename ResourceRecommendController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/news/recommends")
public class ResourceRecommendController {
    private static final Logger logger = LoggerFactory.getLogger(ResourceRecommendController.class);

    @Autowired
    private ResourceRecommendService resourceRecommendService;

    /**
     * 获取推荐列表（返回VO，包含权限信息）
     */
    @GetMapping("/list")
    public ResultDomain<ResourceRecommendVO> getRecommendList() {
        ResultDomain<ResourceRecommendVO> result = resourceRecommendService.getRecommendList();

        return result;
    }

    /**
     * 根据ID获取推荐详情（返回VO，包含权限信息）
     */
    @GetMapping("/recommend/{recommendID}")
    public ResultDomain<ResourceRecommendVO> getRecommendById(@PathVariable("recommendID") String recommendID) {
        TbResourceRecommend filter = new TbResourceRecommend();
        filter.setID(recommendID);
        PageParam pageParam = new PageParam();
        pageParam.setPageNumber(1);
        pageParam.setPageSize(1);
        ResultDomain<ResourceRecommendVO> result = resourceRecommendService.getRecommendPage(filter, pageParam);
        // 从PageDomain中提取dataList的第一个元素
        if (result.getCode() == 200 && result.getPageDomain() != null 
            && result.getPageDomain().getDataList() != null && !result.getPageDomain().getDataList().isEmpty()) {
            ResultDomain<ResourceRecommendVO> singleResult = new ResultDomain<>();
            singleResult.setCode(200);
            singleResult.setMessage(result.getMessage());
            singleResult.setData(result.getPageDomain().getDataList().get(0));
            return singleResult;
        }
        return result;
    }

    /**
     * 创建推荐
     */
    @PostMapping("/recommend")
    public ResultDomain<TbResourceRecommend> createRecommend(@RequestBody TbResourceRecommend recommend) {
        TbSysUser user = LoginUtil.getCurrentUser();
        if (user != null) {
            if (recommend.getCreator() == null) {
                recommend.setCreator(user.getID());
            }
        }
        return resourceRecommendService.addRecommend(recommend);
    }

    /**
     * 更新推荐
     */
    @PutMapping("/recommend")
    public ResultDomain<TbResourceRecommend> updateRecommend(@RequestBody TbResourceRecommend recommend) {
        return resourceRecommendService.updateRecommend(recommend);
    }

    /**
     * 删除推荐
     */
    @DeleteMapping("/recommend/{recommendID}")
    public ResultDomain<Boolean> deleteRecommend(@PathVariable("recommendID") String recommendID) {
        return resourceRecommendService.deleteRecommend(recommendID);
    }

    /**
     * 批量添加推荐资源
     */
    @PostMapping("/recommend/batch")
    public ResultDomain<TbResourceRecommend> batchAddRecommends(@RequestBody Map<String, Object> params) {
        List<String> resourceIDs = (List<String>) params.get("resourceIDs");
        Integer recommendType = (Integer) params.get("recommendType");
        String reason = (String) params.get("reason");
        return resourceRecommendService.batchAddRecommends(resourceIDs, recommendType, reason);
    }

    /**
     * 更新推荐状态
     */
    @PutMapping("/recommend/{recommendID}/status")
    public ResultDomain<TbResourceRecommend> updateRecommendStatus(@PathVariable("recommendID") String recommendID, @RequestParam("status") Integer status) {
        return null;
        // return resourceRecommendService.updateRecommendStatus(recommendID, status);
    }

    /**
     * 更新推荐排序
     */
    @PutMapping("/recommend/{recommendID}/order")
    public ResultDomain<TbResourceRecommend> updateRecommendOrder(@PathVariable("recommendID") String recommendID, @RequestParam("orderNum") Integer orderNum) {
        return resourceRecommendService.updateRecommendOrder(recommendID, orderNum);
    }

    /**
     * 获取活跃推荐
     */
    @GetMapping("/active")
    public ResultDomain<TbResourceRecommend> getActiveRecommends(@RequestParam(required = false, name = "limit") Integer limit) {
        return null;
        // return resourceRecommendService.getActiveRecommends(limit);
    }

    /**
     * 分页查询推荐资源列表
     */
    @PostMapping("/page")
    public ResultDomain<ResourceRecommendVO> getRecommendPage(@RequestBody PageRequest<TbResourceRecommend> request) {
        TbResourceRecommend filter = request.getFilter();
        PageParam pageParam = request.getPageParam();
        return resourceRecommendService.getRecommendPage(filter, pageParam);
    }

    /**
     * 统计推荐资源总数
     */
    @PostMapping("/count")
    public ResultDomain<Long> countRecommends(@RequestBody TbResourceRecommend filter) {
        return resourceRecommendService.countRecommends(filter);
    }

    /**
     * 根据推荐类型获取推荐资源列表
     */
    @GetMapping("/type/{recommendType}")
    public ResultDomain<ResourceRecommendVO> getRecommendsByType(
            @PathVariable("recommendType") Integer recommendType,
            @RequestParam(required = false, name = "limit") Integer limit) {
        return resourceRecommendService.getRecommendsByType(recommendType, limit);
    }

    /**
     * 检查资源是否已推荐（按类型）
     */
    @GetMapping("/check/{resourceID}")
    public ResultDomain<Boolean> isResourceRecommendedByType(
            @PathVariable("resourceID") String resourceID,
            @RequestParam(required = true, name = "recommendType") Integer recommendType) {
        return resourceRecommendService.isResourceRecommendedByType(resourceID, recommendType);
    }
}
