package com.laseroptek.raman.di

import android.content.Context
import androidx.room.Room
import com.laseroptek.raman.data.datasource.db.RamanDatabase
import com.laseroptek.raman.data.source.db.DatabaseService
import com.laseroptek.raman.data.source.db.RamanDatabaseService
import com.laseroptek.raman.repository.DatabaseRepository
import com.laseroptek.raman.utils.DefaultDispatcherProvider
import com.laseroptek.raman.utils.DispatcherProvider
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class DatabaseModule {
    @Provides
    @Singleton
    fun provideDispatcher(): DispatcherProvider = DefaultDispatcherProvider()

    @Singleton
    @Provides
    fun provideDatabaseRepository(databaseService: DatabaseService)= DatabaseRepository(databaseService)

    @Provides
    @Singleton
    fun provideDatabaseService(appDatabase: RamanDatabase): DatabaseService {
        return RamanDatabaseService(appDatabase)
    }
    @Provides
    @Singleton
    fun provideDatabase(
        @ApplicationContext context: Context,
    ): RamanDatabase = Room.databaseBuilder(context, RamanDatabase::class.java, RamanDatabase.DATABASE_NAME)
        // This is not recommended for normal apps, but the goal of this sample isn't to
        // showcase all of Room.
        .fallbackToDestructiveMigration(dropAllTables = true)
        .build()


}