import apiClient from '@/plugins/axios';
import {Transaction} from "@/models/Transaction";
import {format} from "date-fns";
// Импортируете настроенный экземпляр axios

export const getTransaction = async (transactionId: int) => {
    return await apiClient.post(`/transactions/${transactionId}`,);
}

export const getTransactions = async (transaction_type = null, category_type = null, category_id = null) => {
    const params = {};

    // Add the parameters to the params object if they are not null
    if (transaction_type) {
        params.transaction_type = transaction_type;
    }

    if (category_type) {
        params.category_type = category_type;
    }

    if (category_id) {
        params.category_id = category_id;
    }

    // Use axios to make the GET request, passing the params as the second argument
    return await apiClient.get('/transactions/', {
        params: params
    });
}

export const createTransactionRequest = async (transaction: Transaction) => {
    transaction.date = format(transaction.date, 'yyyy-MM-dd')
    return await apiClient.post('/transactions', transaction);
};

export const updateTransactionRequest = async (transaction: Transaction) => {
    const id = transaction.id
    transaction.date = format(transaction.date, 'yyyy-MM-dd')
    return await apiClient.put(`/transactions/${id}`, transaction);
};

export const deleteTransactionRequest = async (id: number) => {
    return await apiClient.delete(`/transactions/${id}`);
};

export const getTransactionTypes = async () => {
    return await apiClient.get('/transactions/types/');
}