package com.bytesw.bytebot.jdbc;

import com.bytesw.bytebot.bean.ActionSummaryByGoalBean;
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.stereotype.Component;

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

@Component
public class ActionJDBCRepository {
    @Autowired
    @Qualifier("sqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    public List<ActionSummaryByGoalBean> getSummaryByGoals(OffsetDateTime startDate, OffsetDateTime endDate) {
        SqlSession session = sqlSessionFactory.openSession();

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("startDate", startDate);
            params.put("endDate", endDate);
            return session.selectList("com.bytesw.bytebot.dao.jdbc.ActionMapper.getSummaryActionsByGoal", params);
        } finally {
            session.close();
        }
    }
}
