package com.easyagents.skill.factory;

import com.easyagents.skill.model.*;
import com.easyagents.skill.util.SkillFrontmatter;

import java.util.List;
import java.util.Map;

/**
 * Skill 标准构建入口。
 */
public final class SkillFactory {

    private SkillFactory() {
    }

    /**
     * 基于 SKILL.md 内容创建 Skill。
     *
     * @param skillContent SKILL.md 原始内容
     * @return Skill 聚合
     */
    public static Skill create(String skillContent) {
        SkillDocument document = SkillFrontmatter.parseDocument(skillContent);
        Map<String, Object> values = document.getFrontmatter().getValues();
        requiredScalar(values, "name");
        requiredScalar(values, "description");
        Skill skill = new Skill();
        skill.setDocument(document);
        return skill;
    }

    /**
     * 基于 SKILL.md 内容和通用资源创建 Skill。
     *
     * @param skillContent SKILL.md 原始内容
     * @param resources 通用资源列表
     * @return Skill 聚合
     */
    public static Skill createWithResources(String skillContent, List<SkillResource> resources) {
        Skill skill = create(skillContent);
        skill.setResources(resources);
        return skill;
    }

    private static void requiredScalar(Map<String, Object> values, String key) {
        Object value = values.get(key);
        if (!(value instanceof String text) || text.isBlank()) {
            throw new com.easyagents.skill.exception.SkillValidationException(
                    "SKILL.md frontmatter " + key + " must be a non-blank string.");
        }
    }
}
