"use server";

import { getGallery, updateGalleryById } from "@/models/gallery.model";
import {
  getPosts as getPostsData,
  createPostMaintain,
  // revertPublish,
  // publishPost as setPostToPublished,
  // toTrashPost,
  // unpublish,
  // updatePostMaintainById,
  searchPosts as searchPostsData,
  // toTrashMultiple,
  getPostByMatchField,
  onUpdating,
  switchLockPost as switchLockPostData,
} from "@/models/posts.model";
import { getCookieByName, setCookie } from "@/utils/cookie";
import { cache } from "react";
import { v6 as uuid } from "uuid";

// Masih dalam pantauan
export const galleryData = async ({ page, limit }) => {
  const data = await getGallery({ page, limit });
  return data;
};

export const updateGallery = async (data) => {
  const result = await updateGalleryById(data);
  return result;
};
// End

export const getPosts = async ({ page, limit }, sort = { createdAt: -1 }) => {
  return await getPostsData({ page, limit }, sort);
};

export const addPost = async (author) => {
  const result = await createPostMaintain({ author });
  return result;
};

// export const updatePost = async (data) => {
//   const result = await updatePostMaintainById(data);
//   return result;
// };

// export const updateMultiplePost = async (data) => {
//   return await Promise.all(data.map(async (_data) => await updatePost(_data)));
// };

// export const publishPost = async (data) => {
//   const result = await setPostToPublished(data);
//   return result;
// };

// export const publishMultiplePost = async (data) => {
//   return await Promise.all(data.map(async (_data) => await publishPost(_data)));
// };

// /**
//  * Revert a published post back to draft status
//  * @param {string} id ID post maintain to revert
//  * @returns {Promise} Resolves with the reverted post data
//  */
// export const revertPublishedPost = async (id) => {
//   const result = await revertPublish(id);
//   return result;
// };

// export const unpublishPost = async (id) => {
//   const result = await unpublish(id);
//   return result;
// };

// export const unpublishMultiplePost = async (data) => {
//   return await Promise.all(
//     data.map(async (_data) => await unpublishPost(_data._id))
//   );
// };

// export const deletePost = async (id) => {
//   const result = await toTrashPost(id);
//   return result;
// };

// export const deleteMultiplePosts = async (ids) => {
//   const result = await toTrashMultiple(ids);
//   return result;
// };

// export const searchPosts = async (keyword) => {
//   const result = await postsServices.searchPostsData(keyword);
//   return result;
// };

export const getPostBySlug = cache(async (slug) => {
  return await getPostByMatchField({ slug, status: "published" }, [], true);
});

export const setOnUpdating = async (id, data = null) => {
  return await onUpdating(id, data);
};

export const switchLockPost = async (post_id, user_id) => {
  // get lockPost
  const uid = uuid();
  const lockPostId = await getCookieByName("lockPost");

  const lockPost = lockPostId || uid;

  if (!lockPostId) {
    const cookieOption = {
      httpOnly: true,
      secure: process.env.NODE_ENV === "production",
      sameSite: "strict",
      path: "/",
    };

    await setCookie("lockPost", uid, cookieOption);
  }

  const result = await switchLockPostData(lockPost, post_id, user_id);
  return result;
};
