package com.example.myapplication.api


import com.example.myapplication.api.model.CategoryRemote
import com.example.myapplication.api.model.ProductRemote
import com.example.myapplication.api.model.UserProductListRemote
import com.example.myapplication.api.model.UserProductRemote
import com.example.myapplication.api.model.UserRemote
import com.example.myapplication.database.entities.Category

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.Interceptor.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query
interface MyServerService {
    @GET("products")
    suspend fun getProducts(
        @Query("_page") page: Int,
        @Query("_limit") limit: Int,
    ): List<ProductRemote>

    @GET("categories")
    suspend fun getCategories(): List<CategoryRemote>

    @POST("products")
    suspend fun createProduct(
        @Body product: ProductRemote,
    ): ProductRemote

    @GET("users")
    suspend fun getAllUsers() : List<UserRemote>

    @GET("users/{id}")
    suspend fun getUserById(@Path("id") id: Int) : UserRemote

    @GET("users")
    suspend fun getUserByAuth(
        @Query("login") login: String,
        @Query("pass") pass: String) : List<UserRemote>

    @GET("users/{id}/userProductCart?_expand=product")
    suspend fun getUserProductCartById(
        @Path("id") id: Int): List<UserProductListRemote>

    @GET("userProductCart")
    suspend fun getProductCart(
        @Query("userId") userId: Int,
        @Query("productId") productId: Int
    ): List<UserProductRemote>


    @POST("users")
    suspend fun createUser(@Body user: UserRemote)

    @POST("userProductCart")
    suspend fun addProductCart(@Body userProduct: UserProductRemote)

    @DELETE("userProductCart/{id}")
    suspend fun deleteCartProduct(
        @Path("id") id: Int,
    )

    companion object {
        private const val BASE_URL = "http://10.0.2.2:8079/"

        @Volatile
        private var INSTANCE: MyServerService? = null

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