package com.github.nullptroma.wallenc.app.di.modules.data

import com.github.nullptroma.wallenc.app.di.modules.app.IoDispatcher
import com.github.nullptroma.wallenc.domain.interfaces.IStorageSyncGroupStore
import com.github.nullptroma.wallenc.infrastructure.android.db.app.dao.StorageKeyMapDao
import com.github.nullptroma.wallenc.infrastructure.android.db.app.dao.StorageSyncGroupDao
import com.github.nullptroma.wallenc.infrastructure.android.db.app.dao.YandexAccountDao
import com.github.nullptroma.wallenc.infrastructure.android.db.app.repository.StorageKeyMapRepository
import com.github.nullptroma.wallenc.infrastructure.android.db.app.repository.StorageSyncGroupRepository
import com.github.nullptroma.wallenc.infrastructure.android.db.app.repository.YandexAccountRepository
import com.github.nullptroma.wallenc.domain.vault.ports.StorageKeyMapStore
import com.github.nullptroma.wallenc.domain.vault.ports.YandexAccountStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Singleton

/**
 * Room DAO/repository types live in `:infrastructure-android` under `domain.vault` packages.
 */
@Module
@InstallIn(SingletonComponent::class)
object PersistenceModule {

    @Provides
    @Singleton
    fun provideStorageKeyMapRepository(
        dao: StorageKeyMapDao,
        @IoDispatcher ioDispatcher: CoroutineDispatcher,
    ): StorageKeyMapRepository = StorageKeyMapRepository(dao, ioDispatcher)

    @Provides
    @Singleton
    fun provideStorageKeyMapStore(impl: StorageKeyMapRepository): StorageKeyMapStore = impl

    @Provides
    @Singleton
    fun provideStorageSyncGroupStore(
        dao: StorageSyncGroupDao,
        @IoDispatcher ioDispatcher: CoroutineDispatcher,
    ): IStorageSyncGroupStore = StorageSyncGroupRepository(dao, ioDispatcher)

    @Provides
    @Singleton
    fun provideYandexAccountRepository(
        dao: YandexAccountDao,
        @IoDispatcher ioDispatcher: CoroutineDispatcher,
    ): YandexAccountRepository = YandexAccountRepository(dao, ioDispatcher)

    @Provides
    @Singleton
    fun provideYandexAccountStore(impl: YandexAccountRepository): YandexAccountStore = impl
}
