import axios from 'axios'; // API base URL const API_BASE_URL = 'http://localhost:8080/api/v1'; // Create axios instance with defaults export const api = axios.create({ baseURL: API_BASE_URL, headers: { 'Content-Type': 'application/json', }, }); // Add auth interceptor api.interceptors.request.use( (config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, (error) => Promise.reject(error) ); // Handle auth errors api.interceptors.response.use( (response) => response, (error) => { if (error.response && error.response.status === 401 && typeof window !== 'undefined') { localStorage.removeItem('token'); window.location.href = '/login'; } return Promise.reject(error); } ); // Auth API export const authApi = { login: async (email: string, password: string) => { const response = await api.post('/auth/login', { email, password }); const { token } = response.data; localStorage.setItem('token', token); return response.data; }, signup: async (name: string, email: string, password: string) => { const response = await api.post('/auth/signup', { name, email, password }); return response.data; }, logout: () => { localStorage.removeItem('token'); window.location.href = '/login'; } }; // Account API export interface Account { id: number; createdAt: string; updatedAt: string; deletedAt: string | null; userID: number; name: string; type: string; balance: number; } export interface AccountInput { name: string; type: string; balance: number; } export const accountApi = { getAccounts: () => api.get('/accounts').then(res => res.data), getAccount: (id: number) => api.get(`/accounts/${id}`).then(res => res.data), createAccount: (account: AccountInput) => api.post('/accounts', account).then(res => res.data), updateAccount: (id: number, account: Partial) => api.put(`/accounts/${id}`, account).then(res => res.data), deleteAccount: (id: number) => api.delete(`/accounts/${id}`).then(res => res.data) }; // Transaction API export interface Transaction { id: number; createdAt: string; updatedAt: string; deletedAt: string | null; userID: number; accountID: number | null; description: string; amount: number; type: "Income" | "Expense"; date: string; category: string; } export interface TransactionInput { description: string; amount: number; type: "Income" | "Expense"; date: string; // YYYY-MM-DD format category: string; accountId?: number; } export interface TransactionFilters { type?: "Income" | "Expense"; accountId?: number; category?: string; startDate?: string; // YYYY-MM-DD format endDate?: string; // YYYY-MM-DD format goalId?: number; } export const transactionApi = { getTransactions: (filters?: TransactionFilters) => { const params: Record = {}; if (filters) { if (filters.type) params.type = filters.type; if (filters.accountId) params.account_id = filters.accountId; if (filters.category) params.category = filters.category; if (filters.startDate) params.start_date = filters.startDate; if (filters.endDate) params.end_date = filters.endDate; if (filters.goalId) params.goal_id = filters.goalId; } return api.get('/transactions', { params }).then(res => res.data); }, getTransaction: (id: number) => api.get(`/transactions/${id}`).then(res => res.data), createTransaction: (transaction: TransactionInput) => api.post('/transactions', transaction).then(res => res.data), updateTransaction: (id: number, transaction: Partial) => api.put(`/transactions/${id}`, transaction).then(res => res.data), deleteTransaction: (id: number) => api.delete(`/transactions/${id}`).then(res => res.data) }; // Goal API export interface Goal { id: number; createdAt: string; updatedAt: string; deletedAt: string | null; userID: number; name: string; targetAmount: number; currentAmount: number; targetDate: string | null; status: "Active" | "Paused" | "Achieved" | "Cancelled"; } export interface GoalProgress { goal: Goal; percentComplete: number; amountRemaining: number; daysRemaining: number; requiredPerDay: number; requiredPerMonth: number; onTrack: boolean; } export interface GoalInput { name: string; targetAmount: number; currentAmount?: number; targetDate?: string; // YYYY-MM-DD format status?: "Active" | "Paused" | "Achieved" | "Cancelled"; } export const goalApi = { getGoals: (status?: "Active" | "Paused" | "Achieved" | "Cancelled") => { const params = status ? { status } : {}; return api.get('/goals', { params }).then(res => res.data); }, getGoal: (id: number) => api.get(`/goals/${id}`).then(res => res.data), createGoal: (goal: GoalInput) => api.post('/goals', goal).then(res => res.data), updateGoal: (id: number, goal: Partial) => api.put(`/goals/${id}`, goal).then(res => res.data), updateGoalProgress: (id: number, currentAmount: number) => api.patch(`/goals/${id}/progress`, { currentAmount }).then(res => res.data), deleteGoal: (id: number) => api.delete(`/goals/${id}`).then(res => res.data), // New goal progress tracking endpoints getGoalProgress: (id: number) => api.get(`/goals/${id}/progress`).then(res => res.data), getAllGoalsProgress: (status?: string) => { const params = status ? { status } : {}; return api.get('/goals/progress/all', { params }).then(res => res.data); }, linkTransactionToGoal: (goalId: number, transactionId: number) => api.post(`/goals/${goalId}/link-transaction`, { transactionId }).then(res => res.data), recalculateGoalProgress: (id: number) => api.post(`/goals/${id}/recalculate`).then(res => res.data) }; // Loan API export interface Loan { id: number; createdAt: string; updatedAt: string; deletedAt: string | null; userID: number; accountID: number | null; name: string; originalAmount: number; currentBalance: number; interestRate: number; startDate: string; endDate: string; } export interface LoanInput { name: string; originalAmount: number; currentBalance: number; interestRate: number; startDate: string; endDate: string; accountId?: number; } export const loanApi = { getLoans: () => api.get('/loans').then(res => res.data), getLoan: (id: number) => api.get(`/loans/${id}`).then(res => res.data), createLoan: (loan: LoanInput) => api.post('/loans', loan).then(res => res.data), updateLoan: (id: number, loan: Partial) => api.put(`/loans/${id}`, loan).then(res => res.data), deleteLoan: (id: number) => api.delete(`/loans/${id}`).then(res => res.data) }; // User API export const userApi = { getProfile: () => api.get('/users/me').then(res => res.data) };