package com.isidroid.c23.di

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.isidroid.c23.data.source.network.AuthInterceptor
import com.isidroid.c23.data.source.network.CacheControlInterceptor
import com.isidroid.c23.utils.DateDeserializer
import com.isidroid.network.ApiCacheControlInterceptor
import com.isidroid.network.ApiServerUrl
import com.isidroid.network.ApiTokenInterceptor
import com.isidroid.session.repository.SessionRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import java.util.Date
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {
    @Singleton @Provides @ApiCacheControlInterceptor
    fun provideApiCacheControlInterceptor(): Interceptor = CacheControlInterceptor()

    @Singleton @Provides @ApiTokenInterceptor
    fun provideAuthInterceptor(sessionRepository: SessionRepository): Interceptor = AuthInterceptor(sessionRepository)

    @Provides @Singleton @ApiServerUrl
    fun provideApiUrl() = "http://stage.teamprinter.com/"

    @Singleton @Provides
    fun provideGson(): Gson = GsonBuilder()
        .setLenient()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZZ")
        .registerTypeAdapter(Date::class.java, DateDeserializer())
        .create()
}