package com.pixelized.biblib.module

import android.content.Context
import android.content.SharedPreferences
import androidx.room.Room
import com.pixelized.biblib.database.BibLibDatabase
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 PersistenceModule {

    @Provides
    @Singleton
    fun provideDatabase(
        @ApplicationContext context: Context,
    ): BibLibDatabase {
        return Room
            .databaseBuilder(
                context,
                BibLibDatabase::class.java,
                BibLibDatabase.DATABASE_NAME
            )
            .fallbackToDestructiveMigration()
            .build()
    }

    @Provides
    @Singleton
    fun providePreferences(
        @ApplicationContext context: Context,
    ): SharedPreferences {
        return context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
    }

    companion object {
        private const val SHARED_PREF = "BIB_LIB_SHARED_PREF"
    }
}