package cn.reghao.oss.console.app.service;

import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.oss.console.account.service.UserContext;
import cn.reghao.oss.console.app.db.repository.UserNodeRepository;
import cn.reghao.oss.console.app.model.dto.UserNodeDto;
import cn.reghao.oss.console.app.model.po.StoreNode;
import cn.reghao.oss.console.app.model.po.UploadChannel;
import cn.reghao.oss.console.app.model.po.UserNode;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-27 09:49:56
 */
@Service
public class UserNodeService {
    private final UserNodeRepository userNodeRepository;
    private final StoreNodeService storeNodeService;
    private final UploadChannelService uploadChannelService;

    public UserNodeService(UserNodeRepository userNodeRepository, StoreNodeService storeNodeService,
                           UploadChannelService uploadChannelService) {
        this.userNodeRepository = userNodeRepository;
        this.storeNodeService = storeNodeService;
        this.uploadChannelService = uploadChannelService;
    }

    public Result add(UserNodeDto userNodeDto) {
        int nodeId = userNodeDto.getNodeId();
        Integer userId = UserContext.getUser().getId();
        UserNode userNode = userNodeRepository.findByNodeIdAndUserId(nodeId, userId);
        if (userNode != null) {
            return Result.fail("UserNode exist");
        }

        StoreNode storeNode = storeNodeService.getById(nodeId);
        if (storeNode == null) {
            return Result.success("StoreNode not exist");
        }

        userNode = new UserNode(userNodeDto, userId);
        userNodeRepository.save(userNode);
        return Result.success("node added");
    }

    public Result delete(Integer id) {
        UserNode userNode = userNodeRepository.findById(id).orElse(null);
        if (userNode != null) {
            int nodeId = userNode.getNodeId();

            int userId = UserContext.getUser().getId();
            List<UploadChannel> uploadChannels = uploadChannelService.getByNodeIdAndUserId(nodeId, userId);
            if (!uploadChannels.isEmpty()) {
                return Result.fail("UploadChannel exists");
            }

            userNodeRepository.delete(userNode);
            return Result.success();
        }

        return Result.fail("UserNode not exists");
    }

    public List<StoreNode> getUserStoreNodes() {
        int loginUser = UserContext.getUser().getId();
        PageRequest pageRequest = PageRequest.of(0, 100);
        List<UserNode> list = userNodeRepository.findByUserId(loginUser, pageRequest).getContent();
        if (list.isEmpty()) {
            return Collections.emptyList();
        }

        List<Integer> nodeIds = list.stream().map(UserNode::getNodeId).collect(Collectors.toList());
        return storeNodeService.getByIds(nodeIds);
    }
}
