package cn.qinys.platform.mobile.service.impl;

import cn.qinys.platform.base.security.utils.CurrentUtils;
import cn.qinys.platform.entity.wishing.UserWish;
import cn.qinys.platform.entity.wishing.WishingTree;
import cn.qinys.platform.mobile.constants.WishtypeEnum;
import cn.qinys.platform.mobile.mapper.UserWishMapper;
import cn.qinys.platform.mobile.mapper.WishingTreeMapper;
import cn.qinys.platform.mobile.req.WishCreateReq;
import cn.qinys.platform.mobile.req.WishPageReq;
import cn.qinys.platform.mobile.resp.WishDetailResp;
import cn.qinys.platform.mobile.resp.WishPageResp;
import cn.qinys.platform.mobile.service.ImageService;
import cn.qinys.platform.mobile.service.UserWishService;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class UserWishServiceImpl extends AbstractUserWishService implements UserWishService {

    @Resource
    private UserWishMapper wishMapper;

    @Resource
    private WishingTreeMapper treeMapper;

    @Resource
    private ImageService imageService;


    @Override
    public Page<WishPageResp> pageByTree(Integer treeId, WishPageReq req) {
        Page<UserWish> pageResp = new Page<>(req.getPage(), req.getPageSize());
        QueryWrapper<UserWish> wrapper = new QueryWrapper<>();
        wrapper.eq("tree_id", treeId)
                .eq("is_public", 1)
                .eq("is_deleted", 0)
                .orderByDesc("created_at");
        return wishMapper.selectWishPage(pageResp, wrapper);
    }

    @Override
    public Page<WishPageResp> pageByUser(WishPageReq req) {
        String userId = CurrentUtils.getCurrentUserId();
        Page<UserWish> pageResp = new Page<>(req.getPage(), req.getPageSize());
        QueryWrapper<UserWish> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("is_deleted", 0)
                .orderByDesc("created_at");
        return wishMapper.selectWishPage(pageResp, wrapper);
    }

    @Override
    public WishDetailResp getDetail(Integer id) {
        return wishMapper.selectWishDetail(id);
    }


    @Override
    public void create(WishCreateReq req) {
        WishingTree tree = treeMapper.selectById(req.getTreeId());
        UserWish wish = new UserWish();
        wish.setTreeId(req.getTreeId());
        wish.setTreeName(tree != null ? tree.getName() : "");
        wish.setUserId(CurrentUtils.getCurrentUserId());
        wish.setContent(req.getContent());
        wish.setImages(JSON.toJSONString(req.getImages()));
        wish.setLongitude(req.getLng());
        wish.setLatitude(req.getLat());
        wish.setAddress(req.getAddress());
        wish.setIsPublic(req.getIsPublic() != null && req.getIsPublic() ? 1 : 0);
        wish.setTags(JSON.toJSONString(req.getTags()));
        wish.setType(req.getType());
        wishMapper.insert(wish);
        this.treeWishesCounter(req.getTreeId(), wish.getIsPublic());
        if (WishtypeEnum.AUTO.getValue().equals(req.getType())) {
            imageService.appendWishImage(wish);
        }
    }

    @Override
    public boolean delete(Long id, String userId) {
        UserWish wish = wishMapper.selectById(id);
        if (wish == null || !wish.getUserId().equals(userId)) {
            return false;
        }
        wishMapper.deleteById(id);
        return true;
    }

    @Override
    public int like(Long id) {
        UserWish wish = wishMapper.selectById(id);
        if (wish == null) {
            return 0;
        }
        wish.setLikes(wish.getLikes() + 1);
        wishMapper.updateById(wish);
        return wish.getLikes();
    }


}
