import ApiService from "@/services/ApiService";

const baseUrl = "lavanderia/ordenes";

/** Crear/actualizar/abrir órdenes puede tardar (BD, ítems, impresión). */
const POS_ORDER_TIMEOUT_MS = 60000;

const orderConfig = { timeout: POS_ORDER_TIMEOUT_MS };

export const getAll = async (params = {}) => {
  const response = await ApiService.get(`${baseUrl}`, { params, ...orderConfig });
  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, ...orderConfig });
  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, orderConfig);
  return response.data;
};

export const updateOrden = async (id, payload) => {
  const response = await ApiService.put(`${baseUrl}/${id}`, payload, orderConfig);
  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,
  }, orderConfig);
  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" });
};

/** Etiqueta de bolsa (cliente + código de barras) directo en impresora térmica. */
export const imprimirEtiquetaBolsaTermica = async (id, { indice, todas = false, posReception = false } = {}) => {
  const body = {};
  if (indice != null) body.indice = indice;
  if (todas) body.todas = true;
  if (posReception) body.pos_reception = true;
  const response = await ApiService.post(`${baseUrl}/${id}/imprimir-etiqueta-bolsa`, body, orderConfig);
  return response.data;
};

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}`, orderConfig);
  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;
};
