package com.gamedog.vididin.di



import com.gamedog.vididin.VidiConst.URL_YOUTUBE_API
import com.gamedog.vididin.core.network.di.GlobalInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.Call
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
internal object NetworkModule {

    @Provides
    @Singleton
    fun providesNetworkJson(): Json = Json {
        ignoreUnknownKeys = true
        isLenient = true
    }

    @Provides
    @Singleton
    fun okHttpCallFactory(): Call.Factory =
        OkHttpClient.Builder()
            .addInterceptor(GlobalInterceptor())
            /*.addInterceptor(
                HttpLoggingInterceptor()
                    .apply {
                        if (BuildConfig.DEBUG) {
                            setLevel(HttpLoggingInterceptor.Level.BODY)
                        }
                    },
            )*/
            .build()

    @Provides
    @Singleton
    fun providesRetrofit(
        okhttpCallFactory: dagger.Lazy<Call.Factory>,
    ): Retrofit {
        return Retrofit.Builder()
            .baseUrl(URL_YOUTUBE_API)
            .callFactory { okhttpCallFactory.get().newCall(it) }
            .addConverterFactory(
                GsonConverterFactory.create()
            )
            .build()
    }
}

