// Third-party Imports
import type { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'

// Type Imports

// Data Imports
import { Order } from '../../types/services/order'

const initialState: { currentOrder: Order } = {
  currentOrder: {
      id: '',
      order_number: '',
      outlet_id: '',
      user_id: '',
      table_number: '',
      order_type: '',
      status: '',
      subtotal: 0,
      tax_amount: 0,
      discount_amount: 0,
      total_amount: 0,
      total_cost: 0,
      remaining_amount: 0,
      payment_status: '',
      refund_amount: 0,
      is_void: false,
      is_refund: false,
      notes: '',
      metadata: {
          customer_name: '',
          last_split_amount: 0,
          last_split_customer_id: '',
          last_split_customer_name: '',
          last_split_payment_id: '',
          last_split_quantities: {},
          last_split_type: ''
      },
      created_at: '',
      updated_at: '',
      order_items: [],
      payments: [],
      total_paid: 0,
      payment_count: 0,
      split_type: ''
  }
}

export const orderSlice = createSlice({
  name: 'order',
  initialState,
  reducers: {
    setOrder: (state, action: PayloadAction<Order>) => {
      state.currentOrder = action.payload
    },
    resetOrder: state => {
      state.currentOrder = initialState.currentOrder
    }
  }
})

export const { setOrder, resetOrder } = orderSlice.actions

export default orderSlice.reducer
