import apiClient from '@/services/axiosSetup';
import {Budget, BudgetCategory} from "@/models/Budget";
// Импортируете настроенный экземпляр axios

export const getBudgetInfos = async () => {

    let response = await apiClient.get('/budgets/');
    let budgetInfos = response.data;
    budgetInfos.forEach((budgetInfo: Budget) => {
        budgetInfo.dateFrom = new Date(budgetInfo.dateFrom);
        budgetInfo.dateTo = new Date(budgetInfo.dateTo);
        // budgetInfo.plannedExpenses.forEach(e => {
        //     e.date = new Date(e.date)
        // })
        //
        // budgetInfo.plannedIncomes.forEach(e => {
        //     e.date = new Date(e.date)
        // })
        //
        // budgetInfo.transactions.forEach(e => {
        //     e.date = new Date(e.date)
        // })
    })
    return budgetInfos
}

export const getBudgetTransactions = async (budgetId, transactionType, categoryType) => {

    let url = `/budgets/${budgetId}/transactions`
    if (transactionType && !categoryType) {
        url += '/?type=' + transactionType
    }
    if (transactionType && categoryType) {
        url += '/'+transactionType+'/'+categoryType
    }
    // if (!categoryType) {
    //     throw new Error('No CategoryType');
    // }
    let response = await apiClient.get(url);
    let transactions = response.data;
    transactions.forEach(e => {
        e.date = new Date(e.date)
    })
    return transactions
}

export const getBudgetCategories = async (budgetId) => {
    let response = await apiClient.get('/budgets/' + budgetId + '/categories/');
    return response.data;
}

export const getBudgetCategoriesSums = async (budgetId) => {
    let response = await apiClient.get('/budgets/' + budgetId + '/categories/_calc_sums');
    return response.data;
}

export const getBudgetInfo = async (budget_id: number) => {

    let budgetInfo = await apiClient.get('/budgets/' + budget_id);
    budgetInfo = budgetInfo.data;

    budgetInfo.dateFrom = new Date(budgetInfo.dateFrom)
    budgetInfo.dateTo = new Date(budgetInfo.dateTo)
    return budgetInfo

};

export const updateBudgetCategoryRequest = async (budget_id, category: BudgetCategory) => {
    await apiClient.put('/budgets/' + budget_id + '/category', category);
}