import {Category, CreateCategoryDTO, UpdateCategoryDTO} from "@/models/category";
import api from "@/network/axiosSetup";

async function fetchCategories(spaceId: number) {
    try {
        let response = await api.get(`/spaces/${spaceId}/categories`);
        return response.data
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function fetchCategory(spaceId: number, categoryId: number): Promise<Category> {
    try {

        let response = await api.get(`/spaces/${spaceId}/categories/${categoryId}`)
        return response.data
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function createCategory(spaceId: number, category: CreateCategoryDTO): Promise<number> {
    try {
        return await api.post(`/spaces/${spaceId}/categories`, category)
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function updateCategory(spaceId: number, categoryId: number, category: UpdateCategoryDTO): Promise<void> {
    try {
        await api.put(`/spaces/${spaceId}/categories/${categoryId}`, category)
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function deleteCategory(spaceId: number, categoryId: number): Promise<void> {
    try {
        await api.delete(`/spaces/${spaceId}/categories/${categoryId}`)
    } catch (error) {
        console.error(error);
        throw error;
    }
}

export const categoriesService = {
    fetchCategories, fetchCategory, createCategory, updateCategory, deleteCategory,
}