package skilldesk.config;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import jakarta.inject.Singleton;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import skilldesk.mapper.MessageMapper;

import javax.sql.DataSource;

@Factory
public class MyBatisConfig {

    @Singleton
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
        Environment environment = new Environment("skilldesk", new JdbcTransactionFactory(), dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(MessageMapper.class);
        return new SqlSessionFactoryBuilder().build(configuration);
    }
}
