package com.tt.honeyDue.network

import io.ktor.client.*
import io.ktor.client.engine.cio.*
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 = "127.0.0.1"

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

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

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

        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.ALL
        }

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

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