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

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

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

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

