package com.tt.honeyDue.network

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import java.util.Locale
import java.util.TimeZone

actual fun getLocalhostAddress(): String = "10.0.2.2"

actual fun getDeviceLanguage(): String {
    return Locale.getDefault().language
}

actual fun getDeviceTimezone(): String {
    return TimeZone.getDefault().id
}

actual fun createHttpClient(): HttpClient {
    return HttpClient(OkHttp) {
        install(ContentNegotiation) {
            json(Json {
                ignoreUnknownKeys = true
                isLenient = true
                prettyPrint = true
            })
        }

        install(Logging) {
            logger = Logger.DEFAULT
            // Only log full request/response bodies in debug builds to avoid
            // leaking auth tokens and PII in production logcat.
            level = if (com.tt.honeyDue.BuildConfig.DEBUG) LogLevel.ALL else LogLevel.INFO
        }

        install(DefaultRequest) {
            headers.append("Accept-Language", getDeviceLanguage())
            headers.append("X-Timezone", getDeviceTimezone())
        }

        // Shared plugins: gzip, retry with backoff, timeouts, token refresh
        installCommonPlugins()
    }
}
