package com.datamate.datamanagement.infrastructure.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.datamate.datamanagement.domain.model.knowledge.KnowledgeItemDirectory;
import com.datamate.datamanagement.infrastructure.persistence.mapper.KnowledgeItemDirectoryMapper;
import com.datamate.datamanagement.infrastructure.persistence.repository.KnowledgeItemDirectoryRepository;
import com.datamate.datamanagement.interfaces.dto.KnowledgeDirectoryQuery;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 知识条目目录仓储实现类
 */
@Repository
@RequiredArgsConstructor
public class KnowledgeItemDirectoryRepositoryImpl
    extends CrudRepository<KnowledgeItemDirectoryMapper, KnowledgeItemDirectory>
    implements KnowledgeItemDirectoryRepository {

    private static final String PATH_SEPARATOR = "/";
    private final KnowledgeItemDirectoryMapper knowledgeItemDirectoryMapper;

    @Override
    public List<KnowledgeItemDirectory> findByCriteria(KnowledgeDirectoryQuery query) {
        String relativePath = normalizeRelativePathPrefix(query.getRelativePath());
        LambdaQueryWrapper<KnowledgeItemDirectory> wrapper = new LambdaQueryWrapper<KnowledgeItemDirectory>()
            .eq(StringUtils.isNotBlank(query.getSetId()), KnowledgeItemDirectory::getSetId, query.getSetId())
            .likeRight(StringUtils.isNotBlank(relativePath), KnowledgeItemDirectory::getRelativePath, relativePath);

        if (StringUtils.isNotBlank(query.getKeyword())) {
            wrapper.and(w -> w.like(KnowledgeItemDirectory::getName, query.getKeyword())
                .or()
                .like(KnowledgeItemDirectory::getRelativePath, query.getKeyword()));
        }

        wrapper.orderByAsc(KnowledgeItemDirectory::getRelativePath);
        return knowledgeItemDirectoryMapper.selectList(wrapper);
    }

    @Override
    public KnowledgeItemDirectory findBySetIdAndPath(String setId, String relativePath) {
        return knowledgeItemDirectoryMapper.selectOne(new LambdaQueryWrapper<KnowledgeItemDirectory>()
            .eq(KnowledgeItemDirectory::getSetId, setId)
            .eq(KnowledgeItemDirectory::getRelativePath, relativePath));
    }

    @Override
    public int removeByRelativePathPrefix(String setId, String relativePath) {
        String normalized = normalizeRelativePathValue(relativePath);
        if (StringUtils.isBlank(normalized)) {
            return 0;
        }
        String prefix = normalizeRelativePathPrefix(normalized);
        LambdaQueryWrapper<KnowledgeItemDirectory> wrapper = new LambdaQueryWrapper<KnowledgeItemDirectory>()
            .eq(KnowledgeItemDirectory::getSetId, setId)
            .and(w -> w.eq(KnowledgeItemDirectory::getRelativePath, normalized)
                .or()
                .likeRight(KnowledgeItemDirectory::getRelativePath, prefix));
        return knowledgeItemDirectoryMapper.delete(wrapper);
    }

    private String normalizeRelativePathPrefix(String relativePath) {
        if (StringUtils.isBlank(relativePath)) {
            return "";
        }
        String normalized = relativePath.replace("\\", PATH_SEPARATOR).trim();
        while (normalized.startsWith(PATH_SEPARATOR)) {
            normalized = normalized.substring(1);
        }
        if (StringUtils.isBlank(normalized)) {
            return "";
        }
        if (!normalized.endsWith(PATH_SEPARATOR)) {
            normalized = normalized + PATH_SEPARATOR;
        }
        return normalized;
    }

    private String normalizeRelativePathValue(String relativePath) {
        if (StringUtils.isBlank(relativePath)) {
            return "";
        }
        String normalized = relativePath.replace("\\", PATH_SEPARATOR).trim();
        while (normalized.startsWith(PATH_SEPARATOR)) {
            normalized = normalized.substring(1);
        }
        while (normalized.endsWith(PATH_SEPARATOR)) {
            normalized = normalized.substring(0, normalized.length() - 1);
        }
        return normalized;
    }
}
