package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.entity.Blacklist;
import com.zhentao.service.BlacklistService;
import com.zhentao.vo.BlacklistVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/blacklist")
@CrossOrigin(origins = "*")
public class BlacklistController {
    
    @Autowired
    private BlacklistService blacklistService;
    
    @PostMapping("/add")
    public Result<Blacklist> addBlacklist(
            @RequestParam Long userId,
            @RequestParam Long blockedUserId,
            @RequestParam(required = false) String reason) {
        try {
            Blacklist blacklist = blacklistService.addBlacklist(userId, blockedUserId, reason);
            return Result.success("添加黑名单成功", blacklist);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("添加黑名单失败");
        }
    }
    
    @DeleteMapping("/remove")
    public Result<Boolean> removeBlacklist(
            @RequestParam Long userId,
            @RequestParam Long blockedUserId) {
        try {
            boolean success = blacklistService.removeBlacklist(userId, blockedUserId);
            if (success) {
                return Result.success("移除黑名单成功", true);
            } else {
                return Result.error("该用户不在黑名单中");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("移除黑名单失败");
        }
    }
    
    @GetMapping("/list")
    public Result<List<BlacklistVO>> getBlacklist(@RequestParam Long userId) {
        try {
            List<BlacklistVO> list = blacklistService.getBlacklist(userId);
            return Result.success(list);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取黑名单列表失败");
        }
    }
    
    @GetMapping("/check")
    public Result<Boolean> isBlocked(
            @RequestParam Long userId,
            @RequestParam Long blockedUserId) {
        try {
            boolean blocked = blacklistService.isBlocked(userId, blockedUserId);
            return Result.success(blocked);
        } catch (Exception e) {
            return Result.error("检查黑名单失败");
        }
    }
    
    @GetMapping("/ids")
    public Result<List<Long>> getBlockedUserIds(@RequestParam Long userId) {
        try {
            List<Long> ids = blacklistService.getBlockedUserIds(userId);
            return Result.success(ids);
        } catch (Exception e) {
            return Result.error("获取黑名单ID列表失败");
        }
    }
    
    @PostMapping("/refresh")
    public Result<Void> refreshCache(@RequestParam Long userId) {
        try {
            blacklistService.refreshCache(userId);
            return Result.success("刷新缓存成功", null);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("刷新缓存失败");
        }
    }
}


