package com.example.smart119.interfaces

import com.example.smart119.MyCookieJar
import okhttp3.Interceptor
import okhttp3.Response

object CookieInterCeptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()

        // 요청 헤더에서 Cookie 확인
        val cookies = request.header("Cookie")

        println("Request URL: ${request.url()}")
        if (cookies != null) {
            println("Cookies in Request: $cookies")
        } else {
            println("No cookies in Request.")

//            val cookies = listOf(
//                "sessionId=abc123",
//                "csrfToken=xyz456",
//                "userId=789"
//            )
            val cookies = MyCookieJar.getCustomCookies()

            if(cookies.size > 0) {
                request = request.newBuilder()
//                    .addHeader("Cookie", "sessionId=abc123")
                    .addHeader("Cookie", cookies.joinToString("; ")) // 수동으로 쿠키 추가
                    .build()
            }

        }

//        request = request.newBuilder()
//            .addHeader("Cookie", "sessionId=abc123")
////                    .addHeader("Cookie", cookies.joinToString("; ")) // 수동으로 쿠키 추가
//            .build()

        return chain.proceed(request)
    }
}