import api from "./client"

export const savingsApi = {
  // Categories
  listCategories: () => api.get("/savings/categories").then((r) => r.data),
  getCategory: (id) => api.get(`/savings/categories/${id}`).then((r) => r.data),
  createCategory: (data) => api.post("/savings/categories", data).then((r) => r.data),
  updateCategory: (id, data) =>
    api.put(`/savings/categories/${id}`, data).then((r) => r.data),
  deleteCategory: (id) => api.delete(`/savings/categories/${id}`),

  // Transactions
  listTransactions: (categoryId, limit = 100, offset = 0) => {
    let url = `/savings/transactions?limit=${limit}&offset=${offset}`
    if (categoryId) url += `&category_id=${categoryId}`
    return api.get(url).then((r) => r.data)
  },
  createTransaction: (data) =>
    api.post("/savings/transactions", data).then((r) => r.data),
  updateTransaction: (id, data) =>
    api.put(`/savings/transactions/${id}`, data).then((r) => r.data),
  deleteTransaction: (id) => api.delete(`/savings/transactions/${id}`),

  // Stats
  getStats: () => api.get("/savings/stats").then((r) => r.data),

  // Members
  getMembers: (categoryId) =>
    api.get(`/savings/categories/${categoryId}/members`).then((r) => r.data),
  addMember: (categoryId, userId) =>
    api
      .post(`/savings/categories/${categoryId}/members`, { user_id: userId })
      .then((r) => r.data),
  removeMember: (categoryId, userId) =>
    api.delete(`/savings/categories/${categoryId}/members/${userId}`),

  // Recurring Plans
  getRecurringPlans: (categoryId) =>
    api
      .get(`/savings/categories/${categoryId}/recurring-plans`)
      .then((r) => r.data),
  createRecurringPlan: (categoryId, data) =>
    api
      .post(`/savings/categories/${categoryId}/recurring-plans`, data)
      .then((r) => r.data),
  updateRecurringPlan: (planId, data) =>
    api.put(`/savings/recurring-plans/${planId}`, data).then((r) => r.data),
  deleteRecurringPlan: (planId) =>
    api.delete(`/savings/recurring-plans/${planId}`),
}
