import { create } from 'zustand'
import api from '../api/axios'

export const useSalaryStore = create((set) => ({
  salaries: [],
  loading: false,
  error: null,

  fetchSalaries: async () => {
    set({ loading: true, error: null })
    try {
      const res = await api.get('/salaries')
      set({ salaries: res.data || [], loading: false })
      return res.data
    } catch (err) {
      set({ loading: false, error: err.message })
      throw err
    }
  },

  createSalary: async (payload) => {
    set({ loading: true, error: null })
    try {
      const res = await api.post('/salaries', payload)
      set({ loading: false })
      return res.data
    } catch (err) {
      set({ loading: false, error: err.message })
      throw err
    }
  },

  updateSalary: async (id, payload) => {
    set({ loading: true, error: null })
    try {
      const res = await api.put(`/salaries/${id}`, payload)
      set({ loading: false })
      return res.data
    } catch (err) {
      set({ loading: false, error: err.message })
      throw err
    }
  },

  deleteSalary: async (id) => {
    set({ loading: true, error: null })
    try {
      const res = await api.delete(`/salaries/${id}`)
      set({ loading: false })
      return res.data
    } catch (err) {
      set({ loading: false, error: err.message })
      throw err
    }
  }
}))

