import { getAuthHeaders } from "./authHeaders";
import { API_BASE } from "./config";

export async function submitSupportRequest({ email, description }) {
  const headers = await getAuthHeaders();
  const res = await fetch(`${API_BASE}/support`, {
    method: "POST",
    headers,
    body: JSON.stringify({ email, description }),
  });
  if (!res.ok) {
    let message = "Failed to send support request";
    try {
      const data = await res.json();
      if (data.message) message = data.message;
      else if (data.error && res.status) message = `${data.error} (${res.status})`;
    } catch {
      message = `Request failed (HTTP ${res.status}). The server did not return JSON — check that POST ${API_BASE}/support is proxied to your Go API (e.g. nginx location /v1/).`;
    }
    throw new Error(message);
  }
  return res.json();
}
