package shop.alien.store.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeBlacklist;
import shop.alien.entity.store.vo.LifeBlacklistVo;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.LifeBlacklistService;

import java.util.List;

/**
 * <p>
 * 黑名单
 * 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-04-30
 */
@Api(tags = {"黑名单"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/life-blacklist")
@RequiredArgsConstructor
public class LifeBlacklistController {

    private final LifeBlacklistService lifeBlacklistService;

    @TrackEvent(
            eventType = "BLOCK",
            eventCategory = "INTERACTION",
            storeId = "",
            userId = "#lifeBlacklist.blockerId",
            targetId = "#lifeBlacklist.blockedId",
            targetType = "STORE"
    )
    @ApiOperation("拉黑")
    @ApiOperationSupport(order = 1)
    @PostMapping("/blackList")
    public R<String> blackList(@RequestBody LifeBlacklist lifeBlacklist) {
        log.info("LifeBlacklistController.blackList?lifeBlacklist={}", lifeBlacklist.toString());
        int num = lifeBlacklistService.blackList(lifeBlacklist);
        if (0 == num) {
            return R.fail("拉黑失败");
        } else if (2 == num) {
            return R.fail("请勿重复拉黑");
        } else {
            return R.data("拉黑成功");
        }
    }

    /**
     * 根据用户ID查询拉黑列表
     *
     * @param userType 用户类型
     * @param userId   用户ID
     * @param type     查找类型 1-被拉黑商户列表 2-拉黑用户列表
     * @return 拉黑列表
     */
    @ApiOperation("拉黑列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/blackListByUserId")
    public R<List<LifeBlacklistVo>> blackListByUserId(@RequestParam(value = "userType") String userType,
                                                      @RequestParam(value = "userId") String userId,
                                                      @RequestParam(required = false, value = "type") Integer type) {
        return R.data(lifeBlacklistService.blackListByUserId(userType, userId, type));
    }

    @ApiOperation("是否为已拉黑")
    @ApiOperationSupport(order = 3)
    @GetMapping("/isBlackListUser")
    public R<Integer> isBlackListUser(@RequestBody LifeBlacklist lifeBlacklist) {
        Boolean blackListUser = lifeBlacklistService.isBlackListUser(lifeBlacklist);
        return R.data(blackListUser ? 1 : 0);
    }

    @ApiOperation("取消拉黑")
    @ApiOperationSupport(order = 4)
    @PostMapping("/cancelBlacklist")
    public R<String> cancelBlacklist(@RequestBody LifeBlacklist lifeBlacklist) {
        log.info("LifeBlacklistController.cancelBlacklist?lifeBlacklist={}", lifeBlacklist.toString());
        int num = lifeBlacklistService.cancelBlacklist(lifeBlacklist);
        if (0 == num) {
            return R.fail("取消拉黑失败");
        } else {
            return R.data("取消拉黑成功");
        }
    }

    @ApiOperation("双方是否存在拉黑关系")
    @ApiOperationSupport(order = 5)
    @GetMapping("/hasBlackList")
    public R<Boolean> hasBlackList(String otherSidePhoneId) throws Exception {
        log.info("LifeBlacklistController.hasBlackList?otherSidePhoneId={}", otherSidePhoneId);
        return R.data(lifeBlacklistService.hasBlackList(otherSidePhoneId));
    }
}
