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 com.bytesw.bytebot.bean.SessionByClientBean;

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

@Component
public class BotSessionJDCBRepository {

    @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.SessionMapper.countSessionsInRange", params);
        } finally {
            session.close();
        }
    }

    public Timestamp getLastDateInRage(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.SessionMapper.getLastDateInRange", params);
        } finally {
            session.close();
        }
    }

    public Double getAvgSessionByCustomerInRange(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.SessionMapper.avgSessionsByCustomerInRange", params);
        } finally {
            session.close();
        }
    }

    public Object getAvgFirstResponseTime(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.SessionMapper.avgFirstResponseTime", params);
        } finally {
            session.close();
        }
    }
}
