"use server";

const apiUrl = process.env.NEXT_SERVER_API_URL;

export async function getTableData() {
  try {
    const res = await fetch(
      `${apiUrl}/VorlageRollenDruck/getAllRollendruckVorlagen`
    );
    if (!res.ok) {
      console.error("Response status:", res.status);
    }

    const data: {
      id: number;
      height: number;
      width: number;
      printer: string;
      articleTypes: string;
    }[] = await res.json();
    return data;
  } catch (error) {
    console.error("Error fetching vorlagen:", error);
    throw new Error("An error occurred while fetching vorlagen.");
  }
}
export async function deleteVorlageRollendruck(id: number) {
  const res = await fetch(`${apiUrl}/VorlageRollenDruck/delete/${id}`, {
    method: "DELETE",
  });

  if (!res.ok) {
    throw new Error("Failed to delete item");
  }

  return;
}

export async function getTableDataDupli() {
  try {
    const res = await fetch(
      `${apiUrl}/VorlageRollenDruck/getAllDupliArtikel`
    );
    if (!res.ok) {
      console.error("Response status:", res.status);
    }

    const data: {
      id: number;
      product_type: string;
    }[] = await res.json();
    return data;
  } catch (error) {
    console.error("Error fetching vorlagen:", error);
    throw new Error("An error occurred while fetching vorlagen.");
  }
}

export async function deleteDupliEntry(id: number) {
  const res = await fetch(`${apiUrl}/VorlageRollenDruck/deleteDupli/${id}`, {
    method: "DELETE",
  });

  if (!res.ok) {
    throw new Error("Failed to delete item");
  }

  return;
}