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

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

export const getTransactions = async (spaceId, transaction_type = null, category_type = null, category_id = null, user_id = null, is_child = null, limit = null, offset = null) => {
    const params = {};
    // params.spaceId=spaceId;

    // 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;
    }
    if (user_id) {
        params.user_id = user_id
    }
    if (is_child!=null){
        params.is_child = is_child
    }
    if (limit) {
        params.limit = limit
    }
    if (offset) {
        params.offset = offset
    }
    const spaceStore = useSpaceStore()
    // Use axios to make the GET request, passing the params as the second argument
    return await apiClient.get(`/spaces/${spaceStore.space?.id}/transactions`, {
        params: params
    });
}

export const createTransactionRequest = async (transaction: Transaction) => {
    const spaceStore = useSpaceStore()
    transaction.date = format(transaction.date, 'yyyy-MM-dd')
    let transactionResponse = await apiClient.post(`/spaces/${spaceStore.space?.id}/transactions`, transaction)

    transaction.date = new Date(transaction.date);
    return transactionResponse.data
};

export const updateTransactionRequest = async (transaction: Transaction) => {
    const spaceStore = useSpaceStore()
    const id = transaction.id
    // console.log(transaction.isDone)
    // transaction.date = transaction.date.setHours(0,0,0,0)
    transaction.date = format(transaction.date, "yyyy-MM-dd")

    const response = await apiClient.put(`/spaces/${spaceStore.space?.id}/transactions/${id}`, transaction);
    transaction = response.data
    transaction.date = new Date(transaction.date);

    return transaction
};

// export const setTransactionDoneRequest = async (transaction: Transaction) => {
//     const id = transaction.id
//     // transaction.date = format(transaction.date, 'yyyy-MM-dd')
//     const response = await apiClient.patch(`/transactions/${id}/set-done`, transaction);
//     // transaction = response.data
//     // transaction.date = new Date(transaction.date);
//
//     // return transaction
// };

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

export const getTransactionTypes = async () => {
    const spaceStore = useSpaceStore()
    return await apiClient.get(`/spaces/${spaceStore.space?.id}/transactions/types`);
}

export const getTransactionCategoriesSums = async () => {
    const spaceStore = useSpaceStore()
    let response = await apiClient.get(`/spaces/${spaceStore.space?.id}/transactions/categories/_calc_sums`);
    return response.data;
}