import { PaginationRequest } from "@/types/globals";
import {
  httpDeleteInterceptor,
  httpGet,
  httpPost,
  httpPut,
} from "./http-config/axios-base-service";
import Cookies from "js-cookie";

const token = Cookies.get("access_token");

export async function createMagazine(data: any) {
  const headers = {
    "content-type": "application/json",
    Authorization: `Bearer ${token}`,
  };
  const pathUrl = `/magazines`;
  return await httpPost(pathUrl, headers, data);
}

export async function getListMagazine(props: PaginationRequest) {
  const { page, limit, search, startDate, endDate } = props;
  const headers = {
    "content-type": "application/json",
  };
  return await httpGet(
    `/magazines?limit=${limit}&page=${page}&title=${search}&startDate=${
      startDate || ""
    }&endDate=${endDate || ""}`,
    headers
  );
}

export async function updateMagazine(id: string, data: any) {
  const headers = {
    "content-type": "application/json",
  };
  const pathUrl = `/magazines/${id}`;
  return await httpPut(pathUrl, headers, data);
}

export async function getMagazineById(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpGet(`/magazines/${id}`, headers);
}

export async function deleteMagazine(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpDeleteInterceptor(`magazines/${id}`, headers);
}

export async function uploadMagazineFile(id: string, data: any) {
  const headers = {
    "content-type": "multipart/form-data",
  };
  return await httpPost(`/magazine-files/${id}`, headers, data);
}

export async function uploadMagazineThumbnail(id: string, data: any) {
  const headers = {
    "content-type": "multipart/form-data",
  };
  return await httpPost(`/magazines/thumbnail/${id}`, headers, data);
}

export async function deleteMagazineFiles(id: number) {
  const headers = {
    "content-type": "multipart/form-data",
  };
  return await httpDeleteInterceptor(`magazine-files/${id}`, headers);
}
