package tech.easyflow.skill.support;

import com.easyagents.skill.factory.SkillFactory;
import com.easyagents.skill.model.SkillMetadata;
import com.easyagents.skill.model.SkillScriptLanguage;
import tech.easyflow.skill.entity.Skill;
import tech.easyflow.skill.entity.SkillAsset;
import tech.easyflow.skill.entity.SkillReference;
import tech.easyflow.skill.entity.SkillScript;

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

/**
 * EasyFlow Skill 实体与 easy-agents-skill 模型转换器。
 */
public final class SkillModelConverter {

    private SkillModelConverter() {
    }

    /**
     * 转换为 easy-agents-skill 聚合。
     *
     * @param skill Skill 主实体
     * @return easy-agents-skill 聚合
     */
    public static com.easyagents.skill.model.Skill toAgentSkill(Skill skill) {
        return SkillFactory.create(
                String.valueOf(skill.getId()),
                skill.getSkillContent(),
                toAgentReferences(skill.getReferences()),
                toAgentScripts(skill.getScripts()),
                toAgentAssets(skill.getAssets())
        );
    }

    /**
     * 转换导入模型到主实体。
     *
     * @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.setMetadataJson(imported.getMetadata().getValues());
        skill.setSkillContent(imported.getSkillContent());
        skill.setReferences(imported.getReferences().stream()
                .map(item -> fromAgentReference(null, null, item))
                .toList());
        skill.setScripts(imported.getScripts().stream()
                .map(item -> fromAgentScript(null, null, item))
                .toList());
        skill.setAssets(imported.getAssets().stream()
                .map(item -> fromAgentAsset(null, null, item))
                .toList());
        skill.setReferenceCount(imported.getReferences().size());
        skill.setScriptCount(imported.getScripts().size());
        skill.setAssetCount(imported.getAssets().size());
        return skill;
    }

    /**
     * 转换 reference 列表。
     *
     * @param references reference 实体
     * @return easy-agents-skill reference
     */
    public static List<com.easyagents.skill.model.SkillReference> toAgentReferences(List<SkillReference> references) {
        return references == null ? List.of() : references.stream().map(item -> {
            com.easyagents.skill.model.SkillReference target = new com.easyagents.skill.model.SkillReference();
            target.setPath(item.getPath());
            target.setName(item.getName());
            target.setContent(item.getContent());
            target.setContentHash(item.getContentHash());
            target.setSize(item.getSize() == null ? 0L : item.getSize());
            target.setMetadata(new SkillMetadata(item.getMetadataJson()));
            return target;
        }).toList();
    }

    /**
     * 转换 script 列表。
     *
     * @param scripts script 实体
     * @return easy-agents-skill script
     */
    public static List<com.easyagents.skill.model.SkillScript> toAgentScripts(List<SkillScript> scripts) {
        return scripts == null ? List.of() : scripts.stream().map(item -> {
            com.easyagents.skill.model.SkillScript target = new com.easyagents.skill.model.SkillScript();
            target.setPath(item.getPath());
            target.setLanguage(parseLanguage(item.getLanguage()));
            target.setContent(item.getContent());
            target.setContentHash(item.getContentHash());
            target.setSize(item.getSize() == null ? 0L : item.getSize());
            target.setMetadata(new SkillMetadata(item.getMetadataJson()));
            return target;
        }).toList();
    }

    /**
     * 转换 asset 列表。
     *
     * @param assets asset 实体
     * @return easy-agents-skill asset
     */
    public static List<com.easyagents.skill.model.SkillAsset> toAgentAssets(List<SkillAsset> assets) {
        return assets == null ? List.of() : assets.stream().map(item -> {
            com.easyagents.skill.model.SkillAsset target = new com.easyagents.skill.model.SkillAsset();
            target.setPath(item.getPath());
            target.setName(item.getName());
            target.setMediaType(item.getMediaType());
            target.setContentRef(item.getContentRef());
            target.setContentHash(item.getContentHash());
            target.setSize(item.getSize() == null ? 0L : item.getSize());
            target.setMetadata(new SkillMetadata(item.getMetadataJson()));
            return target;
        }).toList();
    }

    /**
     * 转换导入 reference。
     *
     * @param skillId Skill ID
     * @param tenantId 租户 ID
     * @param source 导入模型
     * @return reference 实体
     */
    public static SkillReference fromAgentReference(BigInteger skillId, BigInteger tenantId,
                                                   com.easyagents.skill.model.SkillReference source) {
        SkillReference target = new SkillReference();
        target.setTenantId(tenantId);
        target.setSkillId(skillId);
        target.setPath(source.getPath());
        target.setName(source.getName());
        target.setContent(source.getContent());
        target.setContentHash(source.getContentHash());
        target.setSize(source.getSize());
        target.setMetadataJson(source.getMetadata().getValues());
        return target;
    }

    /**
     * 转换导入 script。
     *
     * @param skillId Skill ID
     * @param tenantId 租户 ID
     * @param source 导入模型
     * @return script 实体
     */
    public static SkillScript fromAgentScript(BigInteger skillId, BigInteger tenantId,
                                             com.easyagents.skill.model.SkillScript source) {
        SkillScript target = new SkillScript();
        target.setTenantId(tenantId);
        target.setSkillId(skillId);
        target.setPath(source.getPath());
        target.setLanguage(source.getLanguage().name());
        target.setContent(source.getContent());
        target.setContentHash(source.getContentHash());
        target.setSize(source.getSize());
        target.setMetadataJson(source.getMetadata().getValues());
        return target;
    }

    /**
     * 转换导入 asset。
     *
     * @param skillId Skill ID
     * @param tenantId 租户 ID
     * @param source 导入模型
     * @return asset 实体
     */
    public static SkillAsset fromAgentAsset(BigInteger skillId, BigInteger tenantId,
                                           com.easyagents.skill.model.SkillAsset source) {
        SkillAsset target = new SkillAsset();
        target.setTenantId(tenantId);
        target.setSkillId(skillId);
        target.setPath(source.getPath());
        target.setName(source.getName());
        target.setMediaType(source.getMediaType());
        target.setContentRef(source.getContentRef());
        target.setContentHash(source.getContentHash());
        target.setSize(source.getSize());
        target.setMetadataJson(source.getMetadata().getValues());
        return target;
    }

    private static SkillScriptLanguage parseLanguage(String language) {
        if (language == null || language.isBlank()) {
            return SkillScriptLanguage.UNKNOWN;
        }
        try {
            return SkillScriptLanguage.valueOf(language);
        } catch (IllegalArgumentException ignored) {
            return SkillScriptLanguage.UNKNOWN;
        }
    }
}
