package cn.reghao.tnb.admin.db.mongo;

import cn.reghao.jutil.jdk.web.db.BaseCrud;
import cn.reghao.jutil.jdk.web.db.BaseQuery;
import cn.reghao.tnb.admin.model.po.TnbComment;
import com.mongodb.MongoBulkWriteException;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.InsertManyResult;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2026-01-31 01:00:25
 */
@Repository
public class TnbCommentMongo implements BaseCrud<TnbComment>, BaseQuery<TnbComment> {
    private final String colName = "TnbComment";
    private final MongoTemplate mongoTemplate;
    private final MongoConverter mongoConverter;
    private final int pageSize = 20;

    public TnbCommentMongo(MongoTemplate mongoTemplate, MongoConverter mongoConverter) {
        this.mongoTemplate = mongoTemplate;
        this.mongoConverter = mongoConverter;
    }

    @Override
    public TnbComment save(TnbComment userComment) {
        Document doc = new Document();
        mongoConverter.write(userComment, doc);
        mongoTemplate.getCollection(colName).insertOne(doc);
        return null;
    }

    @Override
    public void saveAll(List<TnbComment> list) {
        List<Document> documents = list.stream()
                .map(t -> {
                    Document doc = new Document();
                    mongoConverter.write(t, doc);
                    return doc;
                })
                .collect(Collectors.toList());

        InsertManyOptions options = new InsertManyOptions();
        // 忽略 insert 失败的文档
        options.ordered(false);
        try {
            InsertManyResult result = mongoTemplate.getCollection(colName).insertMany(documents, options);
        }  catch (MongoBulkWriteException ignore) {
        }
    }

    @Override
    public void update(TnbComment userComment) {
        userComment.setUpdateTime(LocalDateTime.now());

        Document document = new Document();
        mongoConverter.write(userComment, document);
        Document filter = new Document();
        mongoTemplate.getCollection(colName).replaceOne(filter, document);
    }

    @Override
    public void delete(TnbComment userComment) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(userComment.getId()));
        DeleteResult deleteResult = mongoTemplate.remove(query, TnbComment.class);
    }

    public long countByPostId(String postId) {
        Document filter = new Document();
        filter.put("postId", postId);
        filter.put("pCommentId", 0);
        return mongoTemplate.getCollection(colName).countDocuments(filter);
    }

    public List<TnbComment> findByPostId(String postId, long pageNumber) {
        Query query = new Query();
        query.addCriteria(Criteria.where("postId").is(postId));
        query.addCriteria(Criteria.where("pCommentId").is(0));
        query.with(Sort.by(new Sort.Order(Sort.Direction.DESC, "publishAt")));
        query.skip((pageNumber - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, TnbComment.class, colName);
    }

    public long countChildComment(long commentId) {
        Document filter = new Document();
        filter.put("pCommentId", commentId);
        return mongoTemplate.getCollection(colName).countDocuments(filter);
    }

    public List<TnbComment> findByParentId(long commentId, long pageNumber, int pageSize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("pCommentId").is(commentId));
        query.with(Sort.by(new Sort.Order(Sort.Direction.ASC, "publishAt")));
        query.skip((pageNumber - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, TnbComment.class, colName);
    }

    public TnbComment findByCommentId(long commentId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("commentId").is(commentId));
        List<TnbComment> list = mongoTemplate.find(query, TnbComment.class, colName);
        return list.isEmpty() ? null : list.get(0);
    }
}
