import {
    CreateRecurrentOperationDTO,
    RecurrentOperation,
    UpdateRecurrentOperationDTO
} from "@/models/recurrent-operation";
import api from "@/network/axiosSetup";

async function fetchRecurrents(spaceId: number): Promise<RecurrentOperation[]> {
    try {
        let response = await api.get(`/spaces/${spaceId}/recurrents`)
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function fetchRecurrent(spaceId: number, categoryId: number): Promise<RecurrentOperation> {
    try {
        let response = await api.get(`/spaces/${spaceId}/recurrents/${categoryId}`)
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function createRecurrent(spaceId: number, recurrent: CreateRecurrentOperationDTO): Promise<number> {
    try {
        let response = await api.post(`/spaces/${spaceId}/recurrents`, recurrent)
        return response.data;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

async function updateRecurrent(spaceId: number, recurrentId: number, recurrent: UpdateRecurrentOperationDTO): Promise<void> {
    try {
        await api.put(`/spaces/${spaceId}/recurrents/${recurrentId}`, recurrent)
    } catch (error) {
        console.error(error);
        throw error;
    }
}

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


export const recurrentsService = {
    fetchRecurrents, fetchRecurrent, createRecurrent, updateRecurrent, deleteRecurrent,
}