package de.avatic.taric.repository;

import de.avatic.taric.model.Nomenclature;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

@Repository
public interface NomenclatureRepository extends CrudRepository<Nomenclature, Integer> {

    @Query("SELECT * FROM nomenclature WHERE hscode = :hscode AND (end_date IS NULL OR end_date >= CURRENT_DATE)")
    Optional<Nomenclature> findByHscode(String hscode);

    @Query("SELECT * FROM nomenclature WHERE is_leaf = 1 AND hscode LIKE CONCAT(:code, '%') AND (end_date IS NULL OR end_date >= CURRENT_DATE)")
    List<Nomenclature> findDeclarableChildren(@Param("code") String code);
}