import { api } from "@/lib/api"
import type { Post, PostCategory, PostCategoryView, PostComment, PostTag, PostsResponse } from "../types"

// ==================== CATEGORIES ====================

export const getPostCategories = async (admin: boolean = true) => {
    const endpoint = admin ? "/v1/admin/post-categories" : "/v1/post-categories"
    const response = await api.get<PostCategory[]>(endpoint)
    return response.data
}

export const getPostCategoryById = async (id: string, admin: boolean = true) => {
    const endpoint = admin ? `/v1/admin/post-categories/${id}` : `/v1/post-categories/${id}`
    const response = await api.get<PostCategory>(endpoint)
    return response.data
}

export const createPostCategory = async (data: FormData) => {
    const response = await api.post<PostCategory>("/v1/admin/post-categories", data, {
        headers: { "Content-Type": "multipart/form-data" },
    })
    return response.data
}

export const updatePostCategory = async (id: string, data: FormData) => {
    const response = await api.put<PostCategory>(`/v1/admin/post-categories/${id}`, data, {
        headers: { "Content-Type": "multipart/form-data" },
    })
    return response.data
}

export const deletePostCategory = async (id: string) => {
    const response = await api.delete(`/v1/admin/post-categories/${id}`)
    return response.data
}

// ==================== TAGS ====================

export const getPostTags = async (admin: boolean = true) => {
    const endpoint = admin ? "/v1/admin/post-tags" : "/v1/post-tags"
    const response = await api.get<PostTag[]>(endpoint)
    return response.data
}

export const getPostTagById = async (id: string, admin: boolean = true) => {
    const endpoint = admin ? `/v1/admin/post-tags/${id}` : `/v1/post-tags/${id}`
    const response = await api.get<PostTag>(endpoint)
    return response.data
}

export const createPostTag = async (data: { tag: string; is_active?: boolean }) => {
    const response = await api.post<PostTag>("/v1/admin/post-tags", data)
    return response.data
}

export const updatePostTag = async (id: string, data: { tag?: string; is_active?: boolean }) => {
    const response = await api.put<PostTag>(`/v1/admin/post-tags/${id}`, data)
    return response.data
}

export const deletePostTag = async (id: string) => {
    const response = await api.delete(`/v1/admin/post-tags/${id}`)
    return response.data
}

// ==================== POSTS ====================

export interface GetPostsParams {
    admin?: boolean
    front?: boolean
    page?: number
    limit?: number
}

export const getPosts = async ({ admin = true, front, page = 1, limit = 10 }: GetPostsParams = {}) => {
    let endpoint = admin ? "/v1/admin/posts" : "/v1/posts"
    const params = new URLSearchParams()

    if (front !== undefined) {
        params.append("front", String(front))
    }
    params.append("page", String(page))
    params.append("limit", String(limit))

    const queryString = params.toString()
    if (queryString) {
        endpoint += `?${queryString}`
    }

    const response = await api.get<PostsResponse>(endpoint)
    return response.data
}

export const getPostById = async (id: string, admin: boolean = true) => {
    const endpoint = admin ? `/v1/admin/posts/${id}` : `/v1/posts/${id}`
    const response = await api.get<Post>(endpoint)
    return response.data
}

export const createPost = async (data: FormData) => {
    const response = await api.post<Post>("/v1/admin/posts", data, {
        headers: { "Content-Type": "multipart/form-data" },
    })
    return response.data
}

export const updatePost = async (id: string, data: FormData) => {
    const response = await api.put<Post>(`/v1/admin/posts/${id}`, data, {
        headers: { "Content-Type": "multipart/form-data" },
    })
    return response.data
}

export const deletePost = async (id: string) => {
    const response = await api.delete(`/v1/admin/posts/${id}`)
    return response.data
}

// ==================== COMMENTS ====================

export const getPostComments = async (postId?: string, admin: boolean = true) => {
    let endpoint = admin ? "/v1/admin/post-comments" : `/v1/posts/${postId}/comments`
    if (admin && postId) {
        endpoint += `?post_id=${postId}`
    }
    const response = await api.get<PostComment[]>(endpoint)
    return response.data
}

export const getPostCommentById = async (id: string, admin: boolean = true) => {
    const endpoint = admin ? `/v1/admin/post-comments/${id}` : `/v1/post-comments/${id}`
    const response = await api.get<PostComment>(endpoint)
    return response.data
}

// Admin update comment
export const updatePostComment = async (id: string, data: any) => {
    const response = await api.put<PostComment>(`/v1/admin/post-comments/${id}`, data)
    return response.data
}

export const deletePostComment = async (id: string) => {
    const response = await api.delete(`/v1/admin/post-comments/${id}`)
    return response.data
}

// ==================== VIEWS ====================

export const getPostCategoryViews = async (categoryId: string) => {
    const response = await api.get<PostCategoryView[]>(`/v1/admin/post-category-views?category_id=${categoryId}`)
    return response.data
}

// ==================== QUICK CREATE ====================

export const createPostCategoryQuick = async (title: string) => {
    const formData = new FormData()
    formData.append("title", title)
    formData.append("is_active", "true")
    formData.append("order", "0")
    const response = await api.post<PostCategory>("/v1/admin/post-categories", formData, {
        headers: { "Content-Type": "multipart/form-data" },
    })
    return response.data
}

export const createPostTagQuick = async (tag: string) => {
    const response = await api.post<PostTag>("/v1/admin/post-tags", { tag, is_active: true })
    return response.data
}
