package de.tsschulz.tt_tagebuch.shared.di

import de.tsschulz.tt_tagebuch.shared.api.ApiClient
import de.tsschulz.tt_tagebuch.shared.api.SocketService
import de.tsschulz.tt_tagebuch.shared.repository.AuthRepository
import de.tsschulz.tt_tagebuch.shared.repository.DiaryRepository
import de.tsschulz.tt_tagebuch.shared.repository.MemberRepository
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module

import org.koin.core.module.Module
import org.koin.dsl.module

fun initKoin(
    baseUrl: String,
    socketBaseUrl: String? = null,
    additionalModules: List<Module> = emptyList(),
    appDeclaration: KoinAppDeclaration = {},
) =
    startKoin {
        appDeclaration()
        modules(commonModule(baseUrl, socketBaseUrl) + additionalModules)
    }

fun commonModule(baseUrl: String, socketBaseUrl: String? = null) = module {
    single { ApiClient(baseUrl) }
    single {
        SocketService(
            socketBaseUrl
                ?: baseUrl.replace("https://", "wss://").replace("http://", "ws://"),
        )
    }
    single { AuthRepository(get()) }
    single { DiaryRepository(get(), get()) }
    single { MemberRepository(get()) }
}
