import api from './api';
import { type Category } from '../types/Category';

export const categoryService = {
  getAll: async (): Promise<Category[]> => {
    const response = await api.get<Category[]>('/categories');
    return response.data;
  },

  getById: async (id: number): Promise<Category> => {
    const response = await api.get<Category>(`/categories/${id}`);
    return response.data;
  },

  create: async (category: Partial<Category>): Promise<Category> => {
    const response = await api.post<Category>('/categories', category);
    return response.data;
  },

  update: async (id: number, category: Partial<Category>): Promise<void> => {
    await api.put(`/categories/${id}`, category);
  },

  delete: async (id: number): Promise<void> => {
    await api.delete(`/categories/${id}`);
  },

  getOperations: async (id: number): Promise<import('../types/Operation').Operation[]> => {
    const response = await api.get(`/categories/${id}/operations`);
    return response.data;
  },

  addOperation: async (categoryId: number, operationId: number): Promise<void> => {
    await api.post(`/categories/${categoryId}/operations/${operationId}`);
  },

  removeOperation: async (categoryId: number, operationId: number): Promise<void> => {
    await api.delete(`/categories/${categoryId}/operations/${operationId}`);
  }
};
