package com.example.shawarma.data.api

import com.example.shawarma.data.api.models.TokenModelRemote
import com.example.shawarma.data.api.models.UserModelRemote
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.POST


interface MyServerService {
    @POST("register")
    suspend fun createUser(
        @Body user: UserModelRemote,
    ): UserModelRemote

    @POST("auth")
    suspend fun getToken(
        @Body user: UserModelRemote
    ) : TokenModelRemote

    @POST("check/login")
    suspend fun checkLogin(
        @Body user: UserModelRemote
    ) :UserModelRemote?


    companion object {
        private const val BASE_URL = "https://10.0.2.2:80/api/"

        @Volatile
        private var INSTANCE: MyServerService? = null

        fun getInstance(): MyServerService {
            return INSTANCE ?: synchronized(this) {
                val logger = HttpLoggingInterceptor()
                logger.level = HttpLoggingInterceptor.Level.BASIC
                val client = UnsafeOkHttpClient.getUnsafeOkHttpClient()
                return Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
                    .build()
                    .create(MyServerService::class.java)
                    .also { INSTANCE = it }
            }
        }
    }
}