package com.kaixed.kchat.network.interceptor

import com.kaixed.kchat.network.SignUtil.generateSign
import okhttp3.Interceptor
import okhttp3.Response

class SignInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val urlPath = originalRequest.url.encodedPath

        val skipSignPaths = listOf(
            "/users/avatar",
        )

        // 检查是否需要跳过签名
        if (skipSignPaths.any { urlPath.endsWith(it) }) {
            // 直接放行请求，不做签名处理
            return chain.proceed(originalRequest)
        }

        // 获取当前时间戳
        val timestamp = System.currentTimeMillis().toString()

        // 生成请求的签名
        val sign = generateSign(originalRequest, timestamp)

        // 创建新的请求，加入 sign 和 timestamp
        val newRequest = originalRequest.newBuilder()
            .addHeader("sign", sign)
            .addHeader("timestamp", timestamp)
            .build()

        return chain.proceed(newRequest)
    }
}
