import { api, ensureCsrf } from "../../../services/api.js";

/**
 * Offers REST client (authenticated).
 */
export function useOffersApi() {
  async function listOffers(params = {}) {
    await ensureCsrf();
    const { data } = await api.get("/api/offers", { params });
    return data;
  }

  async function getOffer(id) {
    await ensureCsrf();
    const { data } = await api.get(`/api/offers/${id}`);
    return data?.data ?? data;
  }

  async function createOffer(payload) {
    await ensureCsrf();
    const { data } = await api.post("/api/offers", payload);
    return data?.data ?? data;
  }

  async function updateOffer(id, payload) {
    await ensureCsrf();
    const { data } = await api.patch(`/api/offers/${id}`, payload);
    return data?.data ?? data;
  }

  async function deleteOffer(id) {
    await ensureCsrf();
    await api.delete(`/api/offers/${id}`);
  }

  async function sendOffer(id, payload) {
    await ensureCsrf();
    const { data } = await api.post(`/api/offers/${id}/send`, payload);
    return data?.data ?? data;
  }

  async function duplicateFromTemplate(payload) {
    await ensureCsrf();
    const { data } = await api.post("/api/offers/from-template", payload);
    return data?.data ?? data;
  }

  async function convertToProject(id, payload = {}) {
    await ensureCsrf();
    const { data } = await api.post(`/api/offers/${id}/convert-to-project`, payload);
    return data?.data ?? data;
  }

  async function downloadPdf(id) {
    await ensureCsrf();
    const { data } = await api.get(`/api/offers/${id}/pdf`, { responseType: "blob" });
    return data;
  }

  async function editorMeta() {
    await ensureCsrf();
    const { data } = await api.get("/api/offers/meta/editor-context");
    return data;
  }

  async function listCatalog(params = {}) {
    await ensureCsrf();
    const { data } = await api.get("/api/offers/catalog/items", { params });
    return data;
  }

  async function createCatalogRow(payload) {
    await ensureCsrf();
    const { data } = await api.post("/api/offers/catalog/items", payload);
    return data?.data ?? data;
  }

  async function patchCatalogRow(id, payload) {
    await ensureCsrf();
    const { data } = await api.patch(`/api/offers/catalog/items/${id}`, payload);
    return data?.data ?? data;
  }

  async function deleteCatalogRow(id) {
    await ensureCsrf();
    await api.delete(`/api/offers/catalog/items/${id}`);
  }

  async function patchVatPresets(vatRates) {
    await ensureCsrf();
    await api.patch("/api/offers/meta/vat-presets", {
      vat_rate_presets: vatRates,
    });
  }

  async function renameTemplate(offerId, templateName) {
    await ensureCsrf();
    const { data } = await api.patch(`/api/offers/templates/${offerId}`, {
      template_name: templateName,
    });
    return data?.data ?? data;
  }

  /** Public endpoints (guest-friendly when using shared session). */
  async function fetchPublic(uuid) {
    const { data } = await api.get(`/api/offers/public/${uuid}`);
    return data?.data ?? data;
  }

  async function acceptPublic(uuid, payload) {
    await ensureCsrf();
    const { data } = await api.post(
      `/api/offers/public/${uuid}/accept`,
      payload,
    );
    return data?.data ?? data;
  }

  async function declinePublic(uuid, payload) {
    await ensureCsrf();
    const { data } = await api.post(
      `/api/offers/public/${uuid}/decline`,
      payload,
    );
    return data?.data ?? data;
  }

  return {
    listOffers,
    getOffer,
    createOffer,
    updateOffer,
    deleteOffer,
    sendOffer,
    duplicateFromTemplate,
    convertToProject,
    downloadPdf,
    editorMeta,
    listCatalog,
    createCatalogRow,
    patchCatalogRow,
    deleteCatalogRow,
    patchVatPresets,
    renameTemplate,
    fetchPublic,
    acceptPublic,
    declinePublic,
  };
}
