import { apiCall } from "@/utils/callApi";

// Types

export type OrderItem = {
  product_id: string;
  product_sku: string;
  product_name: string;
  price: number;
  quantity: number;
};

export type DeliveryAddress = {
  full_name: string;
  phone: string;
  address_line1: string;
  city: string;
  state: string;
  pincode: string;
  country: string;
};

type OrderIdentifier =
  | { customer_id: string; guest_id?: never }
  | { guest_id: string; customer_id?: never };

export type OrderPayload = OrderIdentifier & {
  customer_name: string;
  customer_phone: string;
  customer_email: string;
  items: OrderItem[];
  delivery_address: DeliveryAddress;
  payment_method: "cod" | "online";
  payment_id?: string;
};

// Place Order (POST)

export function placeOrder(payload: OrderPayload) {
  return apiCall<any>("/clientapi/order", {
    method: "POST",
    body: payload,
  });
}