package tech.easyflow.skill.support;

import com.easyagents.skill.factory.SkillFactory;
import com.easyagents.skill.util.SkillResources;
import tech.easyflow.skill.entity.Skill;
import tech.easyflow.skill.entity.SkillResource;

import java.util.List;

/**
 * EasyFlow Skill 持久化模型与标准 Skill 包模型转换器。
 */
public final class SkillModelConverter {

    private SkillModelConverter() {
    }

    /**
     * 转换为标准 Skill 包聚合。
     *
     * @param skill Skill 主实体
     * @return 标准 Skill 聚合
     */
    public static com.easyagents.skill.model.Skill toAgentSkill(Skill skill) {
        com.easyagents.skill.model.Skill result = SkillFactory.createWithResources(
                skill.getSkillContent(), toAgentResources(skill.getResources()));
        result.setPackageRoot(skill.getName());
        return result;
    }

    /**
     * 将标准 Skill 包聚合转换为待保存实体。
     *
     * @param imported 标准 Skill 聚合
     * @return 待保存 Skill
     */
    public static Skill fromAgentSkill(com.easyagents.skill.model.Skill imported) {
        Skill skill = new Skill();
        skill.setName(imported.getName());
        skill.setDisplayName(imported.getName());
        skill.setDescription(imported.getDescription());
        skill.setSkillContent(imported.getSkillContent());
        skill.setResources(imported.getResources().stream().map(SkillModelConverter::fromAgentResource).toList());
        return skill;
    }

    /**
     * 转换数据库资源为标准包资源。
     *
     * @param resources 数据库资源
     * @return 标准包资源
     */
    public static List<com.easyagents.skill.model.SkillResource> toAgentResources(List<SkillResource> resources) {
        return resources == null ? List.of() : resources.stream().map(source -> {
            com.easyagents.skill.model.SkillResource target = new com.easyagents.skill.model.SkillResource();
            String path = source.getNormalizedPath() == null ? source.getPath() : source.getNormalizedPath();
            target.setPath(path);
            target.setKind(SkillResources.classify(path));
            target.setMediaType(source.getMediaType());
            target.setTextContent(source.getTextContent());
            target.setContentRef(source.getContentRef());
            target.setContentHash(source.getContentHash());
            target.setSize(source.getSize() == null ? 0L : source.getSize());
            return target;
        }).toList();
    }

    /**
     * 转换标准包资源为数据库资源。
     *
     * @param source 标准包资源
     * @return 数据库资源
     */
    public static SkillResource fromAgentResource(com.easyagents.skill.model.SkillResource source) {
        SkillResource target = new SkillResource();
        target.setPath(source.getPath());
        target.setNormalizedPath(source.getPath());
        target.setMediaType(source.getMediaType());
        target.setIsText(source.isText());
        target.setTextContent(source.getTextContent());
        target.setContentRef(source.getContentRef());
        target.setContentHash(source.getContentHash());
        target.setSize(source.getSize());
        return target;
    }
}
