package com.agent.management.repository;

import com.agent.management.model.entity.HermesErrorPattern;
import com.agent.management.repository.json.AbstractJsonRepository;
import com.agent.management.repository.json.JsonStore;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * Hermes 错误嗅探模式仓库（JSON 文件实现）。
 *
 * <p>数据文件：{@code data/json/hermes-error-patterns.json}</p>
 */
@Component
public class HermesErrorPatternRepository extends AbstractJsonRepository<HermesErrorPattern> {

    public HermesErrorPatternRepository(JsonStore store) {
        super(store, new TypeReference<>() {});
    }

    @Override
    protected String getFileName() {
        return "hermes-error-patterns.json";
    }

    @Override
    protected Long getId(HermesErrorPattern entity) {
        return entity.getId();
    }

    @Override
    protected void setId(HermesErrorPattern entity, Long id) {
        entity.setId(id);
    }

    public List<HermesErrorPattern> findAllByEnabledTrue() {
        return findAll(p -> Boolean.TRUE.equals(p.getEnabled()));
    }

    public Optional<HermesErrorPattern> findByName(String name) {
        return findAll().stream().filter(p -> name.equals(p.getName())).findFirst();
    }
}
