import { medusaFetch } from "./medusa";

export interface MedusaCartItem {
  id: string;
  title: string;
  thumbnail: string | null;
  quantity: number;
  unit_price: number;
}

export interface MedusaCart {
  id: string;
  email: string | null;
  currency_code: string;
  item_total: number;
  items: MedusaCartItem[];
}

export interface MedusaShippingOption {
  id: string;
  name: string;
  amount: number;
}

export async function createCart(regionId: string): Promise<MedusaCart> {
  const data = await medusaFetch("/store/carts", {
    method: "POST",
    body: JSON.stringify({ region_id: regionId }),
  });
  return data.cart;
}

export async function getCart(cartId: string): Promise<MedusaCart> {
  const data = await medusaFetch(`/store/carts/${cartId}`);
  return data.cart;
}

export async function addLineItem(cartId: string, variantId: string, quantity = 1): Promise<MedusaCart> {
  const data = await medusaFetch(`/store/carts/${cartId}/line-items`, {
    method: "POST",
    body: JSON.stringify({ variant_id: variantId, quantity }),
  });
  return data.cart;
}

export async function updateLineItem(cartId: string, lineId: string, quantity: number): Promise<MedusaCart> {
  const data = await medusaFetch(`/store/carts/${cartId}/line-items/${lineId}`, {
    method: "POST",
    body: JSON.stringify({ quantity }),
  });
  return data.cart;
}

export async function removeLineItem(cartId: string, lineId: string): Promise<void> {
  await medusaFetch(`/store/carts/${cartId}/line-items/${lineId}`, { method: "DELETE" });
}

interface CartAddress {
  first_name: string;
  last_name: string;
  address_1: string;
  postal_code: string;
  city: string;
  country_code: string;
}

export async function updateCart(
  cartId: string,
  payload: { email?: string; shipping_address?: CartAddress; billing_address?: CartAddress }
): Promise<MedusaCart> {
  const data = await medusaFetch(`/store/carts/${cartId}`, {
    method: "POST",
    body: JSON.stringify(payload),
  });
  return data.cart;
}

export async function listShippingOptions(cartId: string): Promise<MedusaShippingOption[]> {
  const data = await medusaFetch(`/store/shipping-options?cart_id=${cartId}`);
  return data.shipping_options;
}

export async function addShippingMethod(cartId: string, optionId: string): Promise<MedusaCart> {
  const data = await medusaFetch(`/store/carts/${cartId}/shipping-methods`, {
    method: "POST",
    body: JSON.stringify({ option_id: optionId }),
  });
  return data.cart;
}

export async function createPaymentCollection(cartId: string) {
  const data = await medusaFetch("/store/payment-collections", {
    method: "POST",
    body: JSON.stringify({ cart_id: cartId }),
  });
  return data.payment_collection;
}

export async function createPaymentSession(paymentCollectionId: string, providerId: string) {
  const data = await medusaFetch(`/store/payment-collections/${paymentCollectionId}/payment-sessions`, {
    method: "POST",
    body: JSON.stringify({ provider_id: providerId }),
  });
  return data.payment_collection;
}

export async function completeCart(cartId: string): Promise<{ type: "order" | "cart"; order?: any; cart?: any; error?: { message: string } }> {
  return medusaFetch(`/store/carts/${cartId}/complete`, { method: "POST" });
}
