import api from "src/api";
import { createCachedApi } from "./cacheService";

const cachedApi = createCachedApi("company-settings", 300);
const cachedBenefits = createCachedApi("company-benefits", 300);

export const getCompanySettings = async () => {
  const { data } = await cachedApi.get("/company-settings");
  return data.payload;
};

export const updateCompanySettings = async (payload) => {
  const { data } = await cachedApi.put("/company-settings", payload);
  return data.payload;
};

export const uploadCompanyHeroImage = async (file) => {
  const form = new FormData();
  form.append("image", file);
  const { data } = await api.post("/company-settings/hero-image", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  await cachedApi.invalidateAll("/company-settings");
  return data.payload;
};

export const removeCompanyHeroImage = async () => {
  const { data } = await api.delete("/company-settings/hero-image");
  await cachedApi.invalidateAll("/company-settings");
  return data.payload;
};

export const getCompanyBenefits = async () => {
  const { data } = await cachedBenefits.get("/company-benefits");
  return data.payload;
};

export const createCompanyBenefit = async (payload) => {
  const { data } = await api.post("/company-benefits", payload);
  await cachedBenefits.invalidateAll("/company-benefits");
  return data.payload;
};

export const updateCompanyBenefit = async (id, payload) => {
  const { data } = await api.put(`/company-benefits/${id}`, payload);
  await cachedBenefits.invalidateAll("/company-benefits");
  return data.payload;
};

export const reorderCompanyBenefits = async (items) => {
  await api.put("/company-benefits/reorder", items);
  await cachedBenefits.invalidateAll("/company-benefits");
};

export const deleteCompanyBenefit = async (id) => {
  await api.delete(`/company-benefits/${id}`);
  await cachedBenefits.invalidateAll("/company-benefits");
};
