import { query, queryFull, request } from "../utils"
import type { Order } from "../types" // Declare the Order variable

/**
 * Get all orders
 * @returns Promise with array of orders
 */
export const getOrders = async () => {
  return await query("orders", "GET")
}

/**
 * Get order by ID
 * @param id Order ID
 * @returns Promise with order object
 */
export const getOrderById = async (id: number) => {
  return await query(`orders/${id}`, "GET")
}

/**
 * Get orders by student ID
 * @param studentId Student ID
 * @returns Promise with array of orders for the student
 */
export const getOrdersByStudentId = async (studentId: number) => {
  return await query(`orders`, "GET", { student_id: studentId })
}

/**
 * Create a new order
 * @param orderData Order data
 * @returns Promise with newly created order
 */
export const createOrder = async (orderData: any) => {
  return await query("orders", "POST", orderData)
}

/**
 * Update an existing order
 * @param id Order ID
 * @param orderData Updated order data
 * @returns Promise with updated order
 */
export const updateOrder = async (id: number, orderData: any) => {
  return await query(`orders/${id}`, "PUT", orderData)
}

/**
 * Delete an order
 * @param id Order ID
 * @returns Promise with deletion result
 */
export const deleteOrder = async (id: number) => {
  return await query(`orders/${id}`, "DELETE")
}

/**
 * Performs checkout by sending checkout data to the server
 * @param checkoutData The checkout data to send
 * @returns Promise with the checkout response
 */
export const performCheckout = async (checkoutData: any) => {
  return await queryFull("order/checkout", "POST", checkoutData)
}

/**
 * Get order by ID and email for enrollment
 * @param id Order ID
 * @param email Customer email
 * @returns Promise with order object including customer details
 */
export const enrollGetOrders = async (id: number, email: string): Promise<Order> => {
  const response = await request(`order/${id}/${email}`, "GET")
  return response.data
}

/**
 * Calculate order totals and validate order data
 * @param data Order calculation data containing order, order_items, and customer_info
 * @returns Promise with calculation result
 */
export const calculateOrder = async (data: any) => {
  console.log("calculateOrder path:", "order/calculate", "data:", data)

  try {
    const response = await queryFull("order/calculate", "POST", data)
    console.log("[calc][order]", { status: response?.status || "unknown" })
    return response
  } catch (error) {
    console.error("calculateOrder error:", error)
    return { status: "ERROR", message: "Calculation failed" }
  }
}

/**
 * Orders data object for direct access
 * Contains methods for CRUD operations on orders
 */
export const orders = {
  getAll: getOrders,
  getById: getOrderById,
  getByStudentId: getOrdersByStudentId,
  create: createOrder,
  update: updateOrder,
  delete: deleteOrder,
  checkout: performCheckout,
  enrollGet: enrollGetOrders,
  calculate: calculateOrder,
}
