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

// Type Imports

// Data Imports
import { ProductRequest } from '../../types/services/product'

const initialState: { productRequest: ProductRequest } = {
  productRequest: {
    category_id: '',
    sku: '',
    name: '',
    description: '',
    price: 0,
    cost: 0,
    printer_type: '',
    image_url: '',
    variants: []
  }
}

export const productSlice = createSlice({
  name: 'product',
  initialState,
  reducers: {
    setProductField: <K extends keyof ProductRequest>(
      state: Draft<{ productRequest: ProductRequest }>,
      action: PayloadAction<{ field: K; value: ProductRequest[K] }>
    ) => {
      const { field, value } = action.payload
      state.productRequest[field] = value
    },
    setProduct: (state, action: PayloadAction<ProductRequest>) => {
      state.productRequest = action.payload
    },
    resetProduct: state => {
      state.productRequest = initialState.productRequest
    }
  }
})

export const { setProductField, setProduct, resetProduct } = productSlice.actions

export default productSlice.reducer
