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

const defaultHeaders = {
  accept: "application/json",
  language: "tr",
  domain: "evyos.com.tr",
  tz: "GMT+3",
  "Content-type": "application/json",
};

const DefaultResponse = {
  error: "Hata tipi belirtilmedi",
  status: "500",
  data: {},
};

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

const prepareResponse = (response: any, statusCode: number) => {
  try {
    return {
      status: statusCode,
      data: response || {},
    };
  } catch (error) {
    console.error("Error preparing response:", error);
    return {
      ...DefaultResponse,
      error: "Response parsing error",
    };
  }
};

const fetchData = async (
  endpoint: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  try {
    const headers = {
      ...defaultHeaders,
    };
    const fetchOptions: RequestInit = {
      method,
      headers,
      cache: cache ? "force-cache" : "no-cache",
    };

    if (method === "POST" && payload) {
      fetchOptions.body = JSON.stringify(payload);
    }

    const response = await fetch(endpoint, fetchOptions);
    const responseJson = await response.json();
    console.log("Fetching:", endpoint, fetchOptions);
    console.log("Response:", responseJson);
    return prepareResponse(responseJson, response.status);
  } catch (error) {
    console.error("Fetch error:", error);
    return {
      ...DefaultResponse,
      error: error instanceof Error ? error.message : "Network error",
    };
  }
};

const updateDataWithToken = async (
  endpoint: string,
  uuid: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  const accessToken = (await retrieveAccessToken()) || "";
  const headers = {
    ...defaultHeaders,
    "eys-acs-tkn": accessToken,
  };

  try {
    const fetchOptions: RequestInit = {
      method,
      headers,
      cache: cache ? "force-cache" : "no-cache",
    };

    if (method !== "GET" && payload) {
      fetchOptions.body = JSON.stringify(payload.payload);
    }

    const response = await fetch(`${endpoint}/${uuid}`, fetchOptions);
    const responseJson = await response.json();
    console.log("Fetching:", `${endpoint}/${uuid}`, fetchOptions);
    console.log("Response:", responseJson);
    return prepareResponse(responseJson, response.status);
  } catch (error) {
    console.error("Update error:", error);
    return {
      ...DefaultResponse,
      error: error instanceof Error ? error.message : "Network error",
    };
  }
};

const fetchDataWithToken = async (
  endpoint: string,
  payload: any,
  method: string = "POST",
  cache: boolean = false
) => {
  const accessToken = (await retrieveAccessToken()) || "";
  const headers = {
    ...defaultHeaders,
    "eys-acs-tkn": accessToken,
  };

  try {
    const fetchOptions: RequestInit = {
      method,
      headers,
      cache: cache ? "force-cache" : "no-cache",
    };

    if (method === "POST" && payload) {
      fetchOptions.body = JSON.stringify(payload);
    }

    const response = await fetch(endpoint, fetchOptions);
    const responseJson = await response.json();
    console.log("Fetching:", endpoint, fetchOptions);
    console.log("Response:", responseJson);
    return prepareResponse(responseJson, response.status);
  } catch (error) {
    console.error("Fetch with token error:", error);
    return {
      ...DefaultResponse,
      error: error instanceof Error ? error.message : "Network error",
    };
  }
};

export { fetchData, fetchDataWithToken, updateDataWithToken };
