package com.zhentao.controller;

import com.zhentao.dto.BlockDto;
import com.zhentao.service.ChatFriendService;
import com.zhentao.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/chatfriend")
public class ChatFriendController {
    @Autowired
    private ChatFriendService chatFriendService;

    @PostMapping("/block")
    public ResultVo block(@RequestBody BlockDto blockDto){
        return chatFriendService.block(blockDto);
    }

    @GetMapping("/getBlacklist")
    public ResultVo getBlacklist(@RequestParam("userId") Long userId){
        return chatFriendService.getBlacklist(userId);
    }

    @PostMapping("/checkBlock")
    public ResultVo checkBlock(@RequestBody Map<String, String> params) {
        String userId = params.get("userId"); // 自己的ID
        String targetUserId = params.get("targetUserId"); // 对方的ID

        // 查询对方的拉黑列表中是否包含自己
        return chatFriendService.isTargetBlockedMe(targetUserId, userId);
    }
//    我的页面拉黑列表
    @GetMapping("/list")
    public ResultVo list(@RequestParam("userId") Long userId){
        return chatFriendService.getblocklist(userId);
    }

    /**
     * 移除黑名单
     * @param userId 当前用户ID
     * @param blockedUserId 被拉黑的用户ID（对应friendId）
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public ResultVo removeBlacklist(
            @RequestParam("userId") Long userId,
            @RequestParam("blockedUserId") Long blockedUserId) {

        boolean result = chatFriendService.removeFromBlacklist(userId, blockedUserId);
        if (result) {
            return ResultVo.success("移除黑名单成功");
        } else {
            return ResultVo.fail("移除黑名单失败");
        }
    }

    /**
     * 更新好友备注
     * @param params 包含userId、friendId、remark的JSON对象
     * @return 操作结果
     */
    @PostMapping("/updateRemark")
    public ResultVo updateRemark(@RequestBody Map<String, Object> params) {
        Long userId = Long.parseLong(params.get("userId").toString());
        Long friendId = Long.parseLong(params.get("friendId").toString());
        String remark = params.get("remark").toString();
        
        boolean result = chatFriendService.updateRemark(userId, friendId, remark);
        if (result) {
            return ResultVo.success("备注更新成功");
        } else {
            return ResultVo.fail("备注更新失败");
        }
    }

    /**
     * 批量获取好友备注
     * @param params 包含userId和friendIds的JSON对象
     * @return 备注列表
     */
    @PostMapping("/batchGetRemarks")
    public ResultVo batchGetRemarks(@RequestBody Map<String, Object> params) {
        Long userId = Long.parseLong(params.get("userId").toString());
        @SuppressWarnings("unchecked")
        List<String> friendIds = (List<String>) params.get("friendIds");
        
        List<Map<String, Object>> remarks = chatFriendService.batchGetRemarks(userId, friendIds);
        return ResultVo.success("查询成功", remarks);
    }

}
