package tech.easyflow.skill.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tech.easyflow.skill.entity.SkillResource;
import tech.easyflow.skill.mapper.SkillResourceMapper;
import tech.easyflow.skill.service.SkillResourceService;

import java.math.BigInteger;
import java.util.List;

/**
 * Skill 通用资源服务实现。
 */
@Service
public class SkillResourceServiceImpl extends ServiceImpl<SkillResourceMapper, SkillResource>
        implements SkillResourceService {

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SkillResource> listDescriptors(BigInteger skillId, BigInteger tenantId) {
        return list(descriptorQuery(skillId, tenantId));
    }

    /**
     * 构建轻量资源描述查询，避免文件树和管理详情加载全部正文。
     *
     * @param skillId Skill ID
     * @param tenantId 租户 ID
     * @return 资源描述查询
     */
    QueryWrapper descriptorQuery(BigInteger skillId, BigInteger tenantId) {
        return QueryWrapper.create()
                .select("id", "tenant_id", "skill_id", "path", "normalized_path",
                        "media_type", "is_text", "content_hash", "size")
                .eq(SkillResource::getTenantId, tenantId)
                .eq(SkillResource::getSkillId, skillId)
                .orderBy("normalized_path asc");
    }
}
