'use client'

import type { IBaseResponse } from '@/@core/types/api'
import type { IOrder, IOrderItemInput, ORDER_TYPE } from '@/types/data'
import { request } from '@/utils'
import { ORDERS_LIST_QUERY_KEY } from './useGetOrders'
import { useCustomMutation } from '@/hooks/api'

export type CreateOrderPayload = {
  order_type: ORDER_TYPE
  customer_name: string
  customer_phone: string
  customer_email?: string | null
  payment_method?: string
  table_id?: string | null
  delivery_address?: string | Record<string, any>
  delivery_notes?: string | null
  estimated_delivery_time?: string | null
  pickup_time?: string | null
  notes?: string | null
  coupon_code?: string | null
  currency_id?: string | null
  items: IOrderItemInput[]
}

export const useCreateOrder = () => {
  return useCustomMutation<CreateOrderPayload, IBaseResponse<IOrder>>(
    ['orders-create'],
    payload =>
      request({
        url: '/orders',
        method: 'POST',
        data: payload
      }),
    {
      invalidateQueries: [ORDERS_LIST_QUERY_KEY]
    }
  )
}
