package com.wzj.soopin.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.annotation.MemberFillMethod;
import com.wzj.soopin.member.convert.FansConvert;
import com.wzj.soopin.member.convert.MemberBlockConvert;
import com.wzj.soopin.member.domain.bo.FansBO;
import com.wzj.soopin.member.domain.po.Fans;
import com.wzj.soopin.member.domain.po.MemberBlock;
import com.wzj.soopin.member.domain.vo.FansVO;
import com.wzj.soopin.member.domain.vo.MemberBlockVO;
import com.wzj.soopin.member.service.IFansService;
import com.wzj.soopin.member.service.IMemberBlockService;
import com.wzj.soopin.member.service.IMemberService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/ums/fans/")
@RestController
@RequiredArgsConstructor
@Tag(name ="粉丝与关注")
public class FansController {


    private final IMemberService memberService;
    private final IFansService service;

    private final FansConvert convert;

    private final IMemberBlockService memberBlockService;

    private final MemberBlockConvert memberblockconvert;

    @Tag(name ="粉丝列表")
    @PostMapping("/fan/list")
    public R<IPage<FansVO>> fansList(@RequestBody FansBO bo, @RequestBody Page page) {
        LambdaQueryWrapper< Fans> fansQuery = new LambdaQueryWrapper<>();
        fansQuery.eq(Fans::getVloggerId, bo.getMemberId());
        Page<Fans> fans= service.page(page,fansQuery);
        return R.ok(convert.toVO( fans));
    }
    @Tag(name ="关注列表")
    @PostMapping("/follow/list")
    public R<IPage<FansVO>> followList(@RequestBody FansBO bo, @RequestBody Page page) {
        LambdaQueryWrapper< Fans> fansQuery = new LambdaQueryWrapper<>();
        fansQuery.eq(Fans::getFanId, bo.getMemberId());
        Page<Fans> fans= service.page(page,fansQuery);
        return R.ok(convert.toVO( fans));
    }
    @Tag(name ="朋友列表")
    @PostMapping("/friend/list")
    public R<IPage<FansVO>> friendList( @RequestBody FansBO bo, @RequestBody Page page) {
        LambdaQueryWrapper< Fans> fansQuery = new LambdaQueryWrapper<>();
        fansQuery.eq(Fans::getVloggerId, bo.getMemberId());
        fansQuery.eq(Fans::getFriendFlag, 1);
        Page<Fans> fans= service.page(page,fansQuery);
        return R.ok(convert.toVO( fans));
    }


    @Tag(name ="获取详细信息")
    @GetMapping(value = "/{id}")
    public R<FansVO> getInfo(@PathVariable("id") Long id) {
        return R.ok( convert.toVO(service.getById(id)));
    }


    @Tag(name ="删除粉丝信息")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Object> remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }



    @GetMapping("block/list")
    @MemberFillMethod
    public R<List<MemberBlockVO>> queryBlockVloger(@RequestParam Long  myId) {

        List<MemberBlock> blockList= memberBlockService.list(new LambdaQueryWrapper<MemberBlock>().eq(MemberBlock::getMemberId, myId));

        return R.ok(memberblockconvert.toVO(blockList));
    }

    /**
     * 添加黑名单
     *
     * @param myId
     * @param vloggerId
     * @return
     */
    @PostMapping("block")
    public R block(@RequestParam Long myId, @RequestParam Long vloggerId) {
        // 判断两个id不能为空
        if (myId == null || vloggerId == null) {
            return R.ok("id不能为空");
        }

        // 拉黑前只删除“我关注对方”的关系
        service.removeFansRelationship(myId, vloggerId);

        //检查是否已在黑名单中
        boolean hasblock = memberBlockService.hasBlocked(myId, vloggerId);
        if (hasblock) {
            return R.fail("已经拉黑");
        }

        memberBlockService.addBlock(myId, vloggerId);
        return R.ok();
    }

    @PostMapping("cancelBlock")
    public R cancelBlock(@RequestParam Long myId,
                                 @RequestParam Long vloggerId) {
        // 判断两个id不能为空
        if (myId==null||vloggerId==null) {
           R.fail("id不能为空");
        }
       memberBlockService.removeBlock(myId, vloggerId);

        return R.ok();
    }



}
