import ApiService from "@/services/ApiService";

const baseUrl = "lavanderia/ordenes";

export const getAll = async (params = {}) => {
  const response = await ApiService.get(`${baseUrl}`, { params });
  return response.data;
};

/** Órdenes pendientes en recepción (editables en POS). Params: caja_id, mine, limit */
export const getOrdenesAbiertas = async (params = {}) => {
  const response = await ApiService.get(`${baseUrl}/abiertas`, { params });
  return response.data;
};

/** Mismos query params que getAll; respuesta blob CSV (§25). */
export const exportHistorialCsv = async (params = {}) => {
  return ApiService.get(`${baseUrl}/export-csv`, { params, responseType: "blob" });
};

export const createOrden = async (payload) => {
  const response = await ApiService.post(`${baseUrl}`, payload);
  return response.data;
};

export const updateOrden = async (id, payload) => {
  const response = await ApiService.put(`${baseUrl}/${id}`, payload);
  return response.data;
};

/** Sincroniza bolsas de empaque (códigos únicos por bolsa). */
export const syncBolsas = async (id, cantidadBolsas) => {
  const response = await ApiService.put(`${baseUrl}/${id}/bolsas`, {
    cantidad_bolsas: cantidadBolsas,
  });
  return response.data;
};

/** Respuesta axios con blob; revisar content-type antes de abrir como PDF. */
export const fetchBolsasPdfBlob = async (id, options = {}) => {
  const params = {};
  if (options.indice != null) {
    params.indice = options.indice;
  }
  return ApiService.get(`${baseUrl}/${id}/bolsas-pdf`, { params, responseType: "blob" });
};

export const marcarEntregada = async (id) => {
  const response = await ApiService.post(`${baseUrl}/${id}/entregar`);
  return response.data;
};

export const getById = async (id) => {
  const response = await ApiService.get(`${baseUrl}/${id}`);
  return response.data;
};

export const getDashboardResumen = async () => {
  const response = await ApiService.get(`${baseUrl}/dashboard/resumen`);
  return response.data;
};

export const escanearQR = async (codigoQr) => {
  const response = await ApiService.post(`${baseUrl}/escanear-qr`, {
    codigo_qr: codigoQr,
  });
  return response.data;
};

export const escanearFolio = async (folioUnico) => {
  const response = await ApiService.post(`${baseUrl}/escanear-qr`, {
    folio_unico: folioUnico,
  });
  return response.data;
};

export const cancelarOrden = async (id, motivoCancelacion) => {
  const response = await ApiService.post(`${baseUrl}/${id}/cancelar`, {
    motivo_cancelacion: motivoCancelacion,
  });
  return response.data;
};
