import { IFormMemberDisscussionGeneral } from "./type_discussion_general";

export const funGetAllDiscussionGeneral = async (path?: string) => {
   const response = await fetch(`/api/discussion-general${(path) ? path : ''}`, { next: { tags: ['discussion-general'] } });
   return await response.json().catch(() => null);
}

export const funCreateDiscussionGeneral = async (data: { idGroup: string, title: string, desc: string, member: IFormMemberDisscussionGeneral[] }) => {
   const response = await fetch(`/api/discussion-general`, {
      method: "POST",
      headers: {
         "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
   });
   return await response.json().catch(() => null);
}

export const funGetOneDiscussionGeneral = async (id: string, path: string) => {
   const response = await fetch(`/api/discussion-general/${id}${(path) ? path : ''}`, { next: { tags: ['discussion-general'] } });
   return await response.json().catch(() => null);
}

export const funCreateComentDiscussionGeneral = async (path: string, data: { desc: string }) => {
   const response = await fetch(`/api/discussion-general/${path}/comment`, {
      method: "POST",
      headers: {
         "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
   });
   return await response.json().catch(() => null);
}



export const funEditStatusDiscussionGeneral = async (path: string, data: { status: number }) => {
   const response = await fetch(`/api/discussion-general/${path}`, {
      method: "POST",
      headers: {
         "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
   });
   return await response.json().catch(() => null);
}


export const funEditDiscussionGeneral = async (path: string, data: { title: string, desc: string }) => {
   const response = await fetch(`/api/discussion-general/${path}`, {
      method: "PUT",
      headers: {
         "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
   });
   return await response.json().catch(() => null);
}


export const funDeleteDiscussionGeneral = async (path: string, data: { active: boolean }) => {
   const response = await fetch(`/api/discussion-general/${path}`, {
      method: "DELETE",
      headers: {
         "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
   });
   return await response.json().catch(() => null);
}

export const funAddMemberDiscussionGeneral = async (path: string, data: { member: IFormMemberDisscussionGeneral[] }) => {
   const response = await fetch(`/api/discussion-general/${path}/member`, {
      method: "POST",
      headers: {
         "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
   })

   return await response.json().catch(() => null)
}


export const funDelMemberDiscussionGeneral = async (path: string, data: { idUser: string }) => {
   const response = await fetch(`/api/discussion-general/${path}/member`, {
      method: "DELETE",
      headers: {
         "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
   })

   return await response.json().catch(() => null)
}