package de.haushalt.app.data

import de.haushalt.app.BuildConfig
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory

object ApiClient {
    val BASE_URL = BuildConfig.BASE_URL
    private val API_URL = "${BASE_URL}api/"

    private val json = Json {
        ignoreUnknownKeys = true
        explicitNulls = false
    }

    val okHttp: OkHttpClient = OkHttpClient.Builder()
        .addInterceptor(
            HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }
        )
        .build()

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl(API_URL)
        .client(okHttp)
        .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
        .build()

    val taskApi: TaskApi = retrofit.create(TaskApi::class.java)
    val schemaApi: TaskSchemaApi = retrofit.create(TaskSchemaApi::class.java)

    val appUpdateApi: AppUpdateApi = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttp)
        .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
        .build()
        .create(AppUpdateApi::class.java)
}
