package com.swoosh.microblog.data.api

import com.swoosh.microblog.data.model.FileUploadResponse
import com.swoosh.microblog.data.model.GhostSite
import com.swoosh.microblog.data.model.MediaUploadResponse
import com.swoosh.microblog.data.model.MembersResponse
import com.swoosh.microblog.data.model.NewslettersResponse
import com.swoosh.microblog.data.model.PageWrapper
import com.swoosh.microblog.data.model.PagesResponse
import com.swoosh.microblog.data.model.PostWrapper
import com.swoosh.microblog.data.model.PostsResponse
import com.swoosh.microblog.data.model.TagsResponse
import com.swoosh.microblog.data.model.TagWrapper
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import retrofit2.http.*

interface GhostApiService {

    @GET("ghost/api/admin/posts/")
    suspend fun getPosts(
        @Query("limit") limit: Int = 15,
        @Query("page") page: Int = 1,
        @Query("include") include: String = "authors,tags",
        @Query("formats") formats: String = "html,plaintext,mobiledoc",
        @Query("order") order: String = "created_at desc",
        @Query("filter") filter: String? = null
    ): Response<PostsResponse>

    @POST("ghost/api/admin/posts/")
    @Headers("Content-Type: application/json")
    suspend fun createPost(
        @Body body: PostWrapper,
        @Query("newsletter") newsletter: String? = null,
        @Query("email_segment") emailSegment: String? = null
    ): Response<PostsResponse>

    @PUT("ghost/api/admin/posts/{id}/")
    @Headers("Content-Type: application/json")
    suspend fun updatePost(
        @Path("id") id: String,
        @Body body: PostWrapper,
        @Query("newsletter") newsletter: String? = null,
        @Query("email_segment") emailSegment: String? = null
    ): Response<PostsResponse>

    @DELETE("ghost/api/admin/posts/{id}/")
    suspend fun deletePost(
        @Path("id") id: String
    ): Response<Unit>

    @GET("ghost/api/admin/site/")
    suspend fun getSite(): Response<GhostSite>

    @GET("ghost/api/admin/members/")
    suspend fun getMembers(
        @Query("limit") limit: Int = 15,
        @Query("page") page: Int = 1,
        @Query("order") order: String = "created_at desc",
        @Query("filter") filter: String? = null,
        @Query("include") include: String = "newsletters,labels"
    ): Response<MembersResponse>

    @GET("ghost/api/admin/members/{id}/")
    suspend fun getMember(
        @Path("id") id: String,
        @Query("include") include: String = "newsletters,labels"
    ): Response<MembersResponse>

    @GET("ghost/api/admin/newsletters/")
    suspend fun getNewsletters(
        @Query("filter") filter: String = "status:active",
        @Query("limit") limit: String = "all"
    ): Response<NewslettersResponse>

    @GET("ghost/api/admin/users/me/")
    suspend fun getCurrentUser(): Response<UsersResponse>

    // --- Pages ---

    @GET("ghost/api/admin/pages/")
    suspend fun getPages(
        @Query("limit") limit: String = "all",
        @Query("formats") formats: String = "html,plaintext,mobiledoc"
    ): Response<PagesResponse>

    @POST("ghost/api/admin/pages/")
    @Headers("Content-Type: application/json")
    suspend fun createPage(@Body body: PageWrapper): Response<PagesResponse>

    @PUT("ghost/api/admin/pages/{id}/")
    @Headers("Content-Type: application/json")
    suspend fun updatePage(
        @Path("id") id: String,
        @Body body: PageWrapper
    ): Response<PagesResponse>

    @DELETE("ghost/api/admin/pages/{id}/")
    suspend fun deletePage(@Path("id") id: String): Response<Unit>

    // --- Tags ---

    @GET("ghost/api/admin/tags/")
    suspend fun getTags(
        @Query("limit") limit: String = "all",
        @Query("include") include: String = "count.posts"
    ): Response<TagsResponse>

    @GET("ghost/api/admin/tags/{id}/")
    suspend fun getTag(@Path("id") id: String): Response<TagsResponse>

    @POST("ghost/api/admin/tags/")
    @Headers("Content-Type: application/json")
    suspend fun createTag(@Body body: TagWrapper): Response<TagsResponse>

    @PUT("ghost/api/admin/tags/{id}/")
    @Headers("Content-Type: application/json")
    suspend fun updateTag(@Path("id") id: String, @Body body: TagWrapper): Response<TagsResponse>

    @DELETE("ghost/api/admin/tags/{id}/")
    suspend fun deleteTag(@Path("id") id: String): Response<Unit>

    @Multipart
    @POST("ghost/api/admin/images/upload/")
    suspend fun uploadImage(
        @Part file: MultipartBody.Part,
        @Part("purpose") purpose: RequestBody
    ): Response<ImageUploadResponse>

    @Multipart
    @POST("ghost/api/admin/files/upload/")
    suspend fun uploadFile(
        @Part file: MultipartBody.Part,
        @Part("ref") ref: RequestBody? = null
    ): Response<FileUploadResponse>

    @Multipart
    @POST("ghost/api/admin/media/upload/")
    suspend fun uploadMedia(
        @Part file: MultipartBody.Part,
        @Part("ref") ref: RequestBody? = null
    ): Response<MediaUploadResponse>

    @Multipart
    @POST("ghost/api/admin/media/thumbnail/upload/")
    suspend fun uploadMediaThumbnail(
        @Part file: MultipartBody.Part,
        @Part("ref") ref: RequestBody? = null
    ): Response<MediaUploadResponse>
}

data class ImageUploadResponse(
    val images: List<UploadedImage>
)

data class UploadedImage(
    val url: String,
    val ref: String?
)

data class UsersResponse(
    val users: List<GhostUser>
)

data class GhostUser(
    val id: String,
    val name: String?,
    val profile_image: String?,
    val email: String?
)
