package de.avatic.lcc.repositories;

import de.avatic.lcc.database.dialect.SqlDialectProvider;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class NomenclatureRepository {

    private final JdbcTemplate jdbcTemplate;
    private final SqlDialectProvider dialectProvider;

    public NomenclatureRepository(JdbcTemplate jdbcTemplate, SqlDialectProvider dialectProvider) {
        this.jdbcTemplate = jdbcTemplate;
        this.dialectProvider = dialectProvider;
    }

    public List<String> searchHsCode(String search) {
        String concatExpression = dialectProvider.buildConcat("?", "'%'");
        String sql = String.format(
                "SELECT hs_code FROM nomenclature WHERE hs_code LIKE %s ORDER BY hs_code %s",
                concatExpression,
                dialectProvider.buildPaginationClause(10, 0)
        );

        Object[] paginationParams = dialectProvider.getPaginationParameters(10, 0);

        return jdbcTemplate.queryForList(sql, String.class, search, paginationParams[0], paginationParams[1]);
    }

}
