import apiClient from './client'

export interface MaterialCategory {
  id: number
  name: string
  description?: string
  isActive: boolean
  createdAt: string
  sortOrder: number
}

export interface CreateMaterialCategoryRequest {
  name: string
  description?: string
  sortOrder: number
}

export interface UpdateMaterialCategoryRequest {
  name: string
  description?: string
  sortOrder: number
  isActive: boolean
}

export const materialCategoriesApi = {
  async getAll(activeOnly: boolean = true): Promise<MaterialCategory[]> {
    const response = await apiClient.get<MaterialCategory[]>('/materialcategories', {
      params: { activeOnly }
    })
    return response.data
  },

  async getById(id: number): Promise<MaterialCategory> {
    const response = await apiClient.get<MaterialCategory>(`/materialcategories/${id}`)
    return response.data
  },

  async create(data: CreateMaterialCategoryRequest): Promise<MaterialCategory> {
    const response = await apiClient.post<MaterialCategory>('/materialcategories', data)
    return response.data
  },

  async update(id: number, data: UpdateMaterialCategoryRequest): Promise<MaterialCategory> {
    const response = await apiClient.put<MaterialCategory>(`/materialcategories/${id}`, data)
    return response.data
  },

  async delete(id: number): Promise<void> {
    await apiClient.delete(`/materialcategories/${id}`)
  }
}
