// Mock data for orders
export const orders = [
  {
    id: 1,
    student_id: 1,
    total_amount: 2398,
    discount_amount: 500,
    final_amount: 1898,
    status: "completed",
    payment_status: "paid",
    created_at: "2024-04-01T10:15:00Z",
    updated_at: "2024-04-01T10:30:00Z",
  },
  {
    id: 2,
    student_id: 2,
    total_amount: 1699,
    discount_amount: 0,
    final_amount: 1699,
    status: "completed",
    payment_status: "paid",
    created_at: "2024-04-02T14:20:00Z",
    updated_at: "2024-04-02T14:35:00Z",
  },
  {
    id: 3,
    student_id: 3,
    total_amount: 1499,
    discount_amount: 200,
    final_amount: 1299,
    status: "completed",
    payment_status: "paid",
    created_at: "2024-04-03T09:45:00Z",
    updated_at: "2024-04-03T10:00:00Z",
  },
  {
    id: 4,
    student_id: 4,
    total_amount: 1899,
    discount_amount: 0,
    final_amount: 1899,
    status: "pending",
    payment_status: "pending",
    created_at: "2024-04-04T16:30:00Z",
    updated_at: "2024-04-04T16:30:00Z",
  },
  {
    id: 5,
    student_id: 5,
    total_amount: 2199,
    discount_amount: 300,
    final_amount: 1899,
    status: "failed",
    payment_status: "failed",
    created_at: "2024-04-05T11:20:00Z",
    updated_at: "2024-04-05T11:25:00Z",
  },
  {
    id: 6,
    student_id: 6,
    total_amount: 1299,
    discount_amount: 0,
    final_amount: 1299,
    status: "refunded",
    payment_status: "refunded",
    created_at: "2024-04-06T09:10:00Z",
    updated_at: "2024-04-06T14:30:00Z",
  },
  {
    id: 7,
    student_id: 7,
    total_amount: 2499,
    discount_amount: 500,
    final_amount: 1999,
    status: "completed",
    payment_status: "paid",
    created_at: "2024-04-07T13:45:00Z",
    updated_at: "2024-04-07T13:50:00Z",
  },
]

/**
 * Get all orders
 * @returns Array of orders
 */
export const getOrders = () => {
  return orders
}

/**
 * Get order by ID
 * @param id Order ID
 * @returns Order object or undefined if not found
 */
export const getOrderById = (id: number) => {
  return orders.find((order) => order.id === id)
}

/**
 * Get orders by student ID
 * @param studentId Student ID
 * @returns Array of orders for the student
 */
export const getOrdersByStudentId = (studentId: number) => {
  return orders.filter((order) => order.student_id === studentId)
}

/**
 * Create a new order
 * @param orderData Order data
 * @returns Newly created order
 */
export const createOrder = (orderData: any) => {
  const newOrder = {
    id: orders.length + 1,
    ...orderData,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString(),
  }
  orders.push(newOrder)
  return newOrder
}

/**
 * Update an existing order
 * @param id Order ID
 * @param orderData Updated order data
 * @returns Updated order
 */
export const updateOrder = (id: number, orderData: any) => {
  const index = orders.findIndex((order) => order.id === id)
  if (index === -1) {
    throw new Error(`Order with ID ${id} not found`)
  }

  const updatedOrder = {
    ...orders[index],
    ...orderData,
    updated_at: new Date().toISOString(),
  }

  orders[index] = updatedOrder
  return updatedOrder
}

/**
 * Delete an order
 * @param id Order ID
 * @returns Boolean indicating success
 */
export const deleteOrder = (id: number) => {
  const index = orders.findIndex((order) => order.id === id)
  if (index === -1) {
    return false
  }

  orders.splice(index, 1)
  return true
}

/**
 * Performs checkout by simulating a server response
 * @param checkoutData The checkout data to process
 * @returns Promise with the checkout response
 */
export const performCheckout = async (checkoutData: any) => {
  // Simulate API delay
  await new Promise((resolve) => setTimeout(resolve, 1000))

  // Simulate successful checkout
  return {
    status: "OK",
    redirect: "/enroll/receipt?id=123",
  }
}
