package com.jordanbot.autoride.api

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.POST
import okhttp3.Interceptor
import okhttp3.OkHttpClient

import androidx.annotation.Keep

// Data models for the API
@Keep
data class RideLogRequest(
    val platform: String,
    val price: Double,
    val pickupDistance: String,
    val dropoffDistance: String,
    val timeToPickup: String,
    val pickupAddress: String,
    val dropoffAddress: String,
    val isAccepted: Boolean,
    val rawText: String,
    val fingerprint: String,
    val latitude: Double? = null,
    val longitude: Double? = null
)

@Keep
data class LocationPoint(
    val latitude: Double,
    val longitude: Double,
    val speed: Float,
    val timestamp: Long
)

@Keep
data class BulkLocationRequest(
    val fingerprint: String,
    val locations: List<LocationPoint>
)

@Keep
data class ApiResponse(
    val success: Boolean,
    val message: String
)

@Keep
data class CheckSubscriptionRequest(val fingerprint: String)

@Keep
data class CheckSubscriptionResponse(
    val success: Boolean,
    val plan: String?,
    val expires_at: String?,
    val rides_today: Int?,
    val rides_limit: Int?,
    val can_accept: Boolean?
)

@Keep
data class ActivateSubscriptionRequest(
    val fingerprint: String,
    val plan: String,
    val payment_ref: String?
)

@Keep
data class InitPaymentRequest(val fingerprint: String, val plan: String, val amount: Double)

@Keep
data class InitPaymentResponse(
    val success: Boolean,
    val reference_code: String?,
    val amount: Double?,
    val cliq_alias: String?,
    val expires_in_minutes: Int?
)

@Keep
data class CheckPaymentRequest(val reference_code: String, val fingerprint: String)

@Keep
data class CheckPaymentResponse(
    val success: Boolean,
    val status: String // pending, paid, expired
)

interface BackendApiService {
    @POST("api/rides.php")
    suspend fun logRide(@Body request: RideLogRequest): ApiResponse

    @POST("api/location.php")
    suspend fun updateBulkLocation(@Body request: BulkLocationRequest): ApiResponse

    @POST("api/subscription/check.php")
    suspend fun checkSubscription(@Body request: CheckSubscriptionRequest): CheckSubscriptionResponse
    
    @POST("api/subscription/activate.php")
    suspend fun activateSubscription(@Body request: ActivateSubscriptionRequest): ApiResponse

    @POST("api/subscription/init_payment.php")
    suspend fun initPayment(@Body request: InitPaymentRequest): InitPaymentResponse

    @POST("api/subscription/check_payment.php")
    suspend fun checkPayment(@Body request: CheckPaymentRequest): CheckPaymentResponse
}

object ApiClient {
    // Replace with your actual server URL
    private const val BASE_URL = "https://lawer.tripz-egypt.com/jordan_bot/backend/"
    private val API_KEY = com.jordanbot.autoride.BuildConfig.API_KEY
    var appSignature: String = ""

    private val httpClient = OkHttpClient.Builder().apply {
        addInterceptor(Interceptor { chain ->
            val request = chain.request().newBuilder()
                .addHeader("X-API-Key", API_KEY)
                .addHeader("X-App-Signature", appSignature)
                .build()
            chain.proceed(request)
        })
    }.build()

    val service: BackendApiService by lazy {
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(BackendApiService::class.java)
    }
}
