import getXSRFToken from "../getXSRF";

export default async function uploadProfilePhoto(file: File): Promise<{ status: number; ok: boolean; data?: any; error?: string }> {
  const formData = new FormData();
  formData.append("photo", file);

  try {
    const csrfToken = await getXSRFToken();
    const response = await fetch(`${import.meta.env.VITE_API_URL}/api/profile-photo`, {
      method: "POST",
      headers: { "X-XSRF-TOKEN": decodeURIComponent(csrfToken) },
      credentials: "include",
      body: formData,
    });

    const status = response.status;
    let data = null;
    try {
      data = await response.json();
    } catch (e) {
      // no json
    }

    return { status, ok: response.ok, data };
  } catch (err: any) {
    return { status: 0, ok: false, error: err?.message || "Network error" };
  }
}
