package com.servdiary.mobile.data import com.servdiary.mobile.BuildConfig import com.squareup.moshi.Moshi import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Path import retrofit2.http.Query interface ServDiaryApi { @POST("login") suspend fun login(@Body body: LoginRequest): AuthResponse @POST("logout") suspend fun logout() @GET("me") suspend fun me(): AuthResponse @GET("job-appointments") suspend fun appointments( @Query("from") from: String, @Query("to") to: String, ): AppointmentsResponse @GET("jobs") suspend fun jobs(): JobsResponse @GET("jobs/{jobId}/available-slots") suspend fun availableSlots( @Path("jobId") jobId: Int, @Query("date") date: String, ): SlotsResponse @POST("jobs/{jobId}/appointments") suspend fun bookAppointment( @Path("jobId") jobId: Int, @Body body: BookAppointmentRequest, ): AppointmentDto @POST("jobs/{jobId}/appointments/{appointmentId}/check-in") suspend fun checkIn( @Path("jobId") jobId: Int, @Path("appointmentId") appointmentId: Int, @Body body: CheckInRequest, ): CheckInResponse @POST("jobs/{jobId}/appointments/{appointmentId}/complete") suspend fun complete( @Path("jobId") jobId: Int, @Path("appointmentId") appointmentId: Int, ): AppointmentDto } object ApiFactory { fun create(tokenStore: TokenStore): ServDiaryApi { val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() val auth = Interceptor { chain -> val token = tokenStore.token val request = if (token.isNullOrBlank()) { chain.request() } else { chain.request().newBuilder() .header("Authorization", "Bearer $token") .header("Accept", "application/json") .build() } chain.proceed(request) } val logging = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC } val client = OkHttpClient.Builder() .addInterceptor(auth) .addInterceptor(logging) .build() return Retrofit.Builder() .baseUrl(BuildConfig.API_BASE_URL) .client(client) .addConverterFactory(MoshiConverterFactory.create(moshi)) .build() .create(ServDiaryApi::class.java) } }