package com.bytesw.bytebot.jdbc;

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.Map;

@Component
public class ResponseJDBCRepository {

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

    public int countSessionInRange(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.selectOne("com.bytesw.bytebot.dao.jdbc.ResponseMapper.countMessagesInRange", params);
        } finally {
            session.close();
        }
    }
}
