package com.test.bijihoudaun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.test.bijihoudaun.entity.Grouping;
import com.test.bijihoudaun.entity.MarkdownFile;
import com.test.bijihoudaun.entity.TrashItemVo;
import com.test.bijihoudaun.mapper.GroupingMapper;
import com.test.bijihoudaun.mapper.MarkdownFileMapper;
import com.test.bijihoudaun.service.TrashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class TrashServiceImpl implements TrashService {

    @Autowired
    private MarkdownFileMapper markdownFileMapper;

    @Autowired
    private GroupingMapper groupingMapper;

    @Override
    public List<TrashItemVo> getTrashItems() {
        // 调用自定义的Mapper方法查询已删除的笔记，绕过全局逻辑删除过滤器
        List<TrashItemVo> deletedNotes = markdownFileMapper.selectDeleted()
                .stream()
                .map(file -> {
                    TrashItemVo vo = new TrashItemVo();
                    vo.setId(String.valueOf(file.getId()));
                    vo.setTitle(file.getTitle());
                    vo.setType("note");
                    vo.setDeletedAt(file.getDeletedAt());
                    vo.setDeletedBy(String.valueOf(file.getDeletedBy()));
                    return vo;
                })
                .collect(Collectors.toList());

        // 调用自定义的Mapper方法查询已删除的分组
        List<TrashItemVo> deletedGroups = groupingMapper.selectDeleted()
                .stream()
                .map(group -> {
                    TrashItemVo vo = new TrashItemVo();
                    vo.setId(String.valueOf(group.getId()));
                    vo.setTitle(group.getGrouping());
                    vo.setType("group");
                    vo.setDeletedAt(group.getDeletedAt());
                    vo.setDeletedBy(String.valueOf(group.getDeletedBy()));
                    return vo;
                })
                .collect(Collectors.toList());

        // 合并并返回
        return Stream.concat(deletedNotes.stream(), deletedGroups.stream()).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void restoreItem(String id, String type) {
        if ("note".equals(type)) {
            markdownFileMapper.restoreById(Long.parseLong(id));
        } else if ("group".equals(type)) {
            groupingMapper.restoreById(Long.parseLong(id));
        }
    }

    @Override
    @Transactional
    public void permanentlyDeleteItem(String id, String type) {
        if ("note".equals(type)) {
            markdownFileMapper.physicalDeleteById(Long.parseLong(id));
        } else if ("group".equals(type)) {
            // 永久删除分组时，也永久删除其下的所有笔记
            groupingMapper.physicalDeleteById(Long.parseLong(id));
            markdownFileMapper.physicalDeleteByGroupingId(Long.parseLong(id));
        }
    }

    @Override
    @Transactional
    public void cleanTrash() {
        markdownFileMapper.physicalDeleteByIsDeleted();
        groupingMapper.physicalDeleteByIsDeleted();
    }
}