package com.example.quiz.data.remote;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.quiz.utils.SharedPrefsHelper;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.qualifiers.ApplicationContext;
import dagger.hilt.components.SingletonComponent;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
@InstallIn(SingletonComponent.class)
public class ApiClient {
    private static final String DEFAULT_BASE_URL = "https://api-alr.pmu.ml/api/";

    private static final String DEFAULT_REPORT_BASE_URL = "https://dep-alr.pmu.ml/api/v1/";

    private static final String BASE_URL_KEY = "MAIN_API_URL";
    private static final String REPORT_BASE_URL_KEY = "REPORT_API_URL";

    @Provides
    @Singleton
    public AuthInterceptor provideInterceptor(TokenManager tokenManager){
        return new AuthInterceptor(tokenManager);
    }

    @Provides
    @Singleton
    public HttpLoggingInterceptor provideLoggingInterceptor(){
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(@NonNull String s) {
                Log.d("OkHttp", "log: "+s);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }

    @Provides
    @Singleton
    public OkHttpClient provideOkHttpClient(HttpLoggingInterceptor loggingInterceptor, AuthInterceptor authInterceptor){
        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .addInterceptor(authInterceptor)
                .connectTimeout(240, TimeUnit.SECONDS)
                .readTimeout(240, TimeUnit.SECONDS)
                .build();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(OkHttpClient okHttpClient, @ApplicationContext Context context){
        SharedPrefsHelper prefsHelper = SharedPrefsHelper.getInstance(context);
        String BASE_URL = prefsHelper.getString(BASE_URL_KEY, DEFAULT_BASE_URL);
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Provides
    @Singleton
    public ApiService provideApiService(Retrofit retrofit){
        return retrofit.create(ApiService.class);
    }

    @Provides
    @Singleton
    @ReportApiClient
    public Retrofit provideReportRetrofit(OkHttpClient okHttpClient, @ApplicationContext Context context){
        SharedPrefsHelper prefsHelper = SharedPrefsHelper.getInstance(context);
        String REPORT_BASE_URL = prefsHelper.getString(REPORT_BASE_URL_KEY, DEFAULT_REPORT_BASE_URL);
        return new Retrofit.Builder()
                .baseUrl(REPORT_BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Provides
    @Singleton
    public ReportApiService provideReportApiService(@ReportApiClient Retrofit retrofit){
        return retrofit.create(ReportApiService.class);
    }

}
