package com.bytesw.bytebot.jdbc;

import com.bytesw.bytebot.bean.DeploymentChannelParamValueBean;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

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

@Component
public class AgentJDBCRepository {

    @Autowired
    @Qualifier("sqlSessionFactory")
    private SqlSessionFactory sqlsession;

    @Cacheable(value = "deploy_agents")
    public List<DeploymentChannelParamValueBean> getCredentialsFromAgentStatus(String status) {
        SqlSession session = sqlsession.openSession();

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("status", status);
            return session.selectList("com.bytesw.bytebot.dao.jdbc.AgentMapper.getCredentialsFromAgentandChannel", params);

        } finally {
            session.close();
        }

    }
}
