const BASE = "/api/v1/cart";

function authHeaders(token) {
  return {
    "Content-Type": "application/json",
    Authorization: `Bearer ${token}`,
  };
}

async function handleResponse(res) {
  if (res.status === 204) return null;
  const data = await res.json();

  if (!res.ok) throw new Error(data.message || "Cart request failed");
  return data;
}

export async function fetchCart(token, storeId) {
  const res = await fetch(`${BASE}?storeId=${storeId}`, {
    headers: authHeaders(token),
  });
  if (res.status === 204) return null;
  const data = await res.json();

  if (!res.ok) throw new Error(data.message || "Failed to fetch cart");
  return data;
}

export async function apiAddToCart(token, { prodId, storeId, quantity }) {
  const res = await fetch(`${BASE}/add`, {
    method: "POST",
    headers: authHeaders(token),
    body: JSON.stringify({ prodId, storeId, quantity }),
  });
  return handleResponse(res);
}

export async function apiUpdateCartItem(token, { cartItemId, quantity }) {
  const res = await fetch(`${BASE}/update`, {
    method: "PUT",
    headers: authHeaders(token),
    body: JSON.stringify({ cartItemId, quantity }),
  });

  return handleResponse(res);
}

export async function apiRemoveCartItem(token, cartItemId) {
  const res = await fetch(`${BASE}/remove/${cartItemId}`, {
    method: "DELETE",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}

export async function apiClearCart(token, storeId) {
  const res = await fetch(`${BASE}/clear?storeId=${storeId}`, {
    method: "DELETE",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}

export async function apiApplyCoupon(token, storeId, couponCode) {
  const res = await fetch(`${BASE}/apply-coupon?storeId=${storeId}`, {
    method: "POST",
    headers: authHeaders(token),
    body: JSON.stringify({ couponCode }),
  });

  return handleResponse(res);
}

export async function apiApplyPoints(token, storeId, useLoyaltyPoints) {
  const res = await fetch(`${BASE}/apply-points?storeId=${storeId}&useLoyaltyPoints=${useLoyaltyPoints}`, {
    method: "POST",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}

export async function apiRemoveCoupon(token, storeId) {
  const res = await fetch(`${BASE}/coupon?storeId=${storeId}`, {
    method: "DELETE",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}

export async function apiCheckout(token, { storeId }) {
  const res = await fetch(`${BASE}/checkout`, {
    method: "POST",
    headers: authHeaders(token),
    body: JSON.stringify({ storeId }),
  });

  return handleResponse(res);
}

export async function apiCompleteCheckout(token, paymentIntentId) {
  const res = await fetch(`${BASE}/checkout/complete?paymentIntentId=${encodeURIComponent(paymentIntentId)}`, {
    method: "POST",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}

export async function apiCancelCheckout(token, storeId) {
  const res = await fetch(`${BASE}/checkout/cancel?storeId=${storeId}`, {
    method: "POST",
    headers: authHeaders(token),
  });

  return handleResponse(res);
}
