import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'

export const useExpenseStore = defineStore('expense', () => {
  const isLoading = ref(false)
  const error = ref(null)

  async function createExpense(expenseData) {
    isLoading.value = true
    error.value = null
    try {
      const response = await api.post('/expenses/', expenseData)
      return response.data
    } catch (err) {
      error.value = err.response?.data?.detail || err.message
      // Check for DRAFT_LIMIT_REACHED error
      if (err.response?.status === 403 && err.response?.data?.detail === "DRAFT_LIMIT_REACHED") {
        const draftError = new Error('DRAFT_LIMIT_REACHED')
        draftError.response = err.response
        throw draftError
      }
      throw err
    } finally {
      isLoading.value = false
    }
  }

  return {
    isLoading,
    error,
    createExpense,
  }
})