import { unpublish, publishPost } from "@/models/posts.model";
import { response } from "@/utils/helper";

export async function POST(request) {
  try {
    const body = await request.json();
    const { method, data } = body;

    const on = {
      unpublish: async (data) => {
        return Promise.all(data.map(async (id) => await unpublish(id)));
      },
      publish: async (data) => {
        return Promise.all(data.map(async (_data) => await publishPost(_data)));
      },
    };

    const result = await on[method](data);

    return response(200, `${method} post successfully`, result);
  } catch (error) {
    return response(500, "Internal Server Error", null, {
      error: "Failed to update data",
    });
  }
}
