"use client";

import { api } from "@/app/services/config/api.config";
import { getData } from "./helper";

export const postsServices = {
  getPosts: async ({ page = 1, limit = 100 }, sort, search) => {
    const response = await getData(
      "/admin/posts",
      { page, limit },
      sort,
      search
    );
    return response;
  },

  searchPosts: async (keyword) => {
    const response = await getData(
      "/admin/posts",
      { page: 1, limit: 200 },
      { createdAt: -1 },
      keyword
    );

    return response.data;
  },

  updatePost: async (data) => {
    const response = await api.patch("/admin/posts", data);
    return response.data;
  },

  publishPost: async (data) => {
    const response = await api.post("/admin/posts/publish", {
      method: "publish",
      data: [data],
    });
    return response.data[0];
  },

  unpublishPost: async (id) => {
    const response = await api.post("/admin/posts/publish", {
      method: "unpublish",
      data: [id],
    });
    return response.data[0];
  },

  deletePost: async (id) => {
    const response = await api.post("/admin/posts/delete", [id]);
    return response.data[0];
  },

  revertPublish: async (id) => {
    const response = await api.post("/admin/posts/revert", { id });
    return response.data;
  },

  publishMultiplePost: async (data) => {
    const response = await api.post("/admin/posts/publish", {
      method: "publish",
      data,
    });
    return response.data;
  },

  unpublishMultiplePost: async (data) => {
    const response = await api.post("/admin/posts/publish", {
      method: "unpublish",
      data,
    });
    return response.data;
  },

  deleteMultiplePosts: async (data) => {
    const response = await api.post("/admin/posts/delete", data);
    return response.data;
  },
};
