"use server";

import { retrieveAccessToken } from "@/apicalls/cookies/token";

const defaultHeaders = {
  accept: "application/json",
  "Content-type": "application/json",
};

const DefaultResponse = {
  completed: false,
  error: "Hata tipi belirtilmedi",
  message: "Hata oluştu, lütfen tekrar deneyin",
  status: "500",
  data: {},
};

interface HeadersObject {
  cache: string;
  method: string;
  headers: Object;
  body?: string;
}

const cacheList = ["no-cache", "no-store", "force-cache", "only-if-cached"];

const prepareResponse = async (response: any) => {
  try {
    const responseJson = await response.json();
    const statusResponse = response?.status;
    const errorResponse = responseJson?.error || responseJson?.Error;
    const messageResponse = (responseJson?.message || "").toString();
    const completeResponse = responseJson?.completed || false;

    const preparedResponse = {
      completed: completeResponse,
      message: messageResponse,
      status: statusResponse,
      error: errorResponse || "",
      ...responseJson,
    };

    return preparedResponse;
  } catch (error) {}
  return DefaultResponse;
};

const fetchData = async (
  endpoint: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  let headersObject: any = {
    cache: cache ? "force-cache" : "no-cache",
    method: method,
    headers: defaultHeaders,
  };
  if (method !== "GET") {
    headersObject = {
      ...headersObject,
      body: JSON.stringify(payload),
    };
  }
  console.log("headersObject", headersObject);
  try {
    const response = await fetch(endpoint, headersObject);
    return await prepareResponse(response);
  } catch (error) {}
  return DefaultResponse;
};

const updateDataWithToken = async (
  endpoint: string,
  uuid: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  const accessToken = (await retrieveAccessToken()) || "";
  let headersObject: any = {
    cache: cache ? "force-cache" : "no-cache",
    method: method,
    headers: {
      ...defaultHeaders,
      "evyos-session-key": accessToken,
    },
  };

  if (method !== "GET") {
    headersObject = {
      ...headersObject,
      body: JSON.stringify(payload.payload),
    };
  }

  try {
    const response = await fetch(`${endpoint}/${uuid}`, headersObject);
    return await prepareResponse(response);
  } catch (error) {}
  return DefaultResponse;
};

const fetchDataWithToken = async (
  endpoint: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  const accessToken = (await retrieveAccessToken()) || "";
  let headersObject: any = {
    cache: cache ? "force-cache" : "no-cache",
    method: method,
    headers: {
      ...defaultHeaders,
      "evyos-session-key": accessToken,
    },
  };
  if (method !== "GET") {
    headersObject = {
      ...headersObject,
      body: JSON.stringify(payload),
    };
  }
  try {
    const response = await fetch(endpoint, headersObject);
    return await prepareResponse(response);
  } catch (error) {}
  return DefaultResponse;
};

export { fetchData, fetchDataWithToken, updateDataWithToken };
