package com.isidroid.session.di

import android.content.Context
import com.google.gson.Gson
import com.isidroid.core.DiDebuggableBuild
import com.isidroid.core.DiMock
import com.isidroid.network.ApiCacheControlInterceptor
import com.isidroid.network.ApiServerUrl
import com.isidroid.network.ApiTokenInterceptor
import com.isidroid.network.createRetrofitApiClient
import com.isidroid.session.data.source.network.ApiSession
import com.isidroid.session.data.source.network.MockInterceptor
import com.isidroid.session.repository.SessionRepository
import com.isidroid.session.repository.SessionRepositoryImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Qualifier
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object SessionModule {
    @Provides
    @Singleton
    fun provideSessionRepository(): SessionRepository = SessionRepositoryImpl()

    @Provides
    @DiSessionMockInterceptor
    fun provideMockInterceptor(
        @ApplicationContext context: Context,
        @DiMock isMock: Boolean
    ): Interceptor = MockInterceptor(context, isMock)

    @Singleton
    @Provides
    fun provideApiSession(
        @ApiServerUrl serverUrl: String,
        @DiDebuggableBuild isDebugBuild: Boolean,
        @ApiTokenInterceptor authInterceptor: Interceptor,
        @DiSessionMockInterceptor mockInterceptor: Interceptor,
        @ApiCacheControlInterceptor cacheInterceptor: Interceptor,
        gson: Gson
    ) = createRetrofitApiClient(
        baseUrl = serverUrl,
        cl = ApiSession::class.java,
        logLevel = HttpLoggingInterceptor.Level.BODY,
        gson = gson,
        isDebugBuild = isDebugBuild,
        interceptors = listOf(authInterceptor, mockInterceptor, cacheInterceptor)
    )
}

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER)
annotation class DiSessionMockInterceptor