"use server";

import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { revalidatePath, revalidateTag } from "next/cache";
import { BomWeightingScoreResult } from ".";

export interface UpdateBomWeightingScoreInputs {
  id: number;
  name: string;
  range: number;
  weighting: number;
  remarks?: string;
}

export const updateBomWeightingScore = async (data: UpdateBomWeightingScoreInputs) => {
  const response = await serverFetchJson<BomWeightingScoreResult>(
    `${BASE_API_URL}/bomWeightingScores/${data.id}`,
    {
      method: "PUT",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag("bomWeightingScores");
  revalidatePath("/(main)/settings/bomWeighting");

  return response;
};
