package com.zaojiao.app.core.http.interceptor

import android.content.Context
import android.os.Build
import dagger.hilt.android.qualifiers.ApplicationContext
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Inject

class VersionInterceptor @Inject constructor(
    @ApplicationContext private val application: Context,
) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val newRequest = chain.request().newBuilder()

        val packageName = application.packageName
        val packageInfo = application.packageManager.getPackageInfo(packageName, 0)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            newRequest.addHeader("version", packageInfo.longVersionCode.toString())
        } else {
            newRequest.addHeader("version", packageInfo.versionCode.toString())
        }
        newRequest.addHeader("platform", "android")
        newRequest.addHeader("packageName", packageName)
        newRequest.addHeader("package", packageName)

        return chain.proceed(newRequest.build())
    }
}