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

// Type Imports

// Data Imports
import { IngredientItem } from '../../types/services/ingredient'

const initialState: { currentIngredient: IngredientItem } = {
  currentIngredient: {
    id: '',
    organization_id: '',
    outlet_id: '',
    name: '',
    unit_id: '',
    cost: 0,
    stock: 0,
    is_semi_finished: false,
    is_active: true,
    metadata: {},
    created_at: '',
    updated_at: '',
    unit: {
      id: '',
      organization_id: '',
      outlet_id: '',
      name: '',
      abbreviation: '',
      is_active: false,
      created_at: '',
      updated_at: ''
    }
  }
}

export const ingredientSlice = createSlice({
  name: 'ingredient',
  initialState,
  reducers: {
    setIngredient: (state, action: PayloadAction<IngredientItem>) => {
      state.currentIngredient = action.payload
    },
    resetIngredient: state => {
      state.currentIngredient = initialState.currentIngredient
    }
  }
})

export const { setIngredient, resetIngredient } = ingredientSlice.actions

export default ingredientSlice.reducer
