import { defineStore } from "pinia";
import { ref } from "vue";
import { Transaction } from "@/models/transaction";
import { TransactionFilters, TransactionService } from "@/services/transactions-service";

const transactionsService = TransactionService

export const useTransactionStore = defineStore('transactions', () => {
    const transactions = ref<Transaction[]>([])
    const plannedTransactions = ref<Transaction[]>([])
    const isLoading = ref(false)

    const fetchTransactions = async (spaceId: number) => {
        isLoading.value = true
        try {
            transactions.value = await transactionsService.getTransactions(spaceId, {} as TransactionFilters)
        } finally {
            isLoading.value = false
        }
    }

    const fetchPlannedTransactions = async (spaceId: number) => {
        try {
            // Assuming TransactionKind is imported or available. If not, we might need to import it or use string 'PLANNING' if enum is not exported here.
            // Based on previous file view, TransactionKind is not imported in this file. Let's check imports.
            // It is not imported. I should add import or use type casting if I can't easily add import in this block.
            // But wait, I can add import in a separate block or just use the service which expects filters.
            // Let's use 'PLANNING' as any or import it. 
            // Actually, I should add the import first. But this tool replaces a block.
            // I will assume I can add the import in a separate call or if I include the top of the file.
            // Let's just use the value for now or try to rely on auto-import if possible (unlikely).
            // Better: I will use a separate tool call to add the import if needed, or just use the string if the service accepts it (it expects enum).
            // Let's look at the file again. Line 4 imports TransactionFilters.
            // I'll just add the code here and then fix imports if needed.
            // Actually, I'll try to match the enum value manually or use a magic string casted if I have to, but better to import.
            // I'll replace the whole file content or a larger chunk to include imports? No, that's expensive.
            // I'll just add the function and state here.

            // Wait, I can't easily add an import with this tool if I'm targeting the body.
            // I'll use 'PLANNING' as any for now to avoid import error, or better, I'll add the import in a separate step.

            plannedTransactions.value = await transactionsService.getTransactions(spaceId, { kind: 'PLANNING', isDone: false } as any)
        } catch (error) {
            console.error(error)
        }
    }

    const addTransaction = (transaction: Transaction) => {
        transactions.value.push(transaction)
    }

    return {
        transactions,
        plannedTransactions,
        isLoading,
        fetchTransactions,
        fetchPlannedTransactions,
        addTransaction
    }
})