// Flashcards API service for handling flashcard operations export interface CardSet { id: string; name: string; description: string; color: string; cardCount: number; createdAt: string; updatedAt: string; progress: number; lastStudied: string; tags: string[]; } export interface Flashcard { id: string; word: string; translation: string; definition: string; partOfSpeech: string; pronunciation: string; example: string; exampleTranslation?: string; masteryLevel: number; timesReviewed: number; isFavorite: boolean; nextReviewDate: string; createdAt: string; cardSet: { name: string; color: string; }; } export interface CreateCardSetRequest { name: string; description: string; isPublic?: boolean; } export interface CreateFlashcardRequest { cardSetId: string; english: string; chinese: string; pronunciation: string; partOfSpeech: string; example: string; } export interface ApiResponse { success: boolean; data?: T; error?: string; message?: string; } const API_BASE_URL = 'http://localhost:5000'; class FlashcardsService { private getAuthToken(): string | null { if (typeof window === 'undefined') return null; return localStorage.getItem('auth_token'); } private async makeRequest( endpoint: string, options: RequestInit = {} ): Promise { const token = this.getAuthToken(); const url = `${API_BASE_URL}/api${endpoint}`; const response = await fetch(url, { headers: { 'Content-Type': 'application/json', ...(token && { 'Authorization': `Bearer ${token}` }), ...options.headers, }, ...options, }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: 'Network error' })); throw new Error(errorData.error || errorData.details || `HTTP ${response.status}`); } return response.json(); } // CardSets methods async getCardSets(): Promise> { try { return await this.makeRequest>('/cardsets'); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch card sets', }; } } async createCardSet(data: CreateCardSetRequest): Promise> { try { return await this.makeRequest>('/cardsets', { method: 'POST', body: JSON.stringify(data), }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to create card set', }; } } async deleteCardSet(id: string): Promise> { try { return await this.makeRequest>(`/cardsets/${id}`, { method: 'DELETE', }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to delete card set', }; } } // Flashcards methods async getFlashcards(cardSetId?: string): Promise> { try { const query = cardSetId ? `?cardSetId=${cardSetId}` : ''; return await this.makeRequest>(`/flashcards${query}`); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to fetch flashcards', }; } } async createFlashcard(data: CreateFlashcardRequest): Promise> { try { return await this.makeRequest>('/flashcards', { method: 'POST', body: JSON.stringify(data), }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to create flashcard', }; } } async updateFlashcard(id: string, data: Partial): Promise> { try { return await this.makeRequest>(`/flashcards/${id}`, { method: 'PUT', body: JSON.stringify(data), }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to update flashcard', }; } } async deleteFlashcard(id: string): Promise> { try { return await this.makeRequest>(`/flashcards/${id}`, { method: 'DELETE', }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to delete flashcard', }; } } async toggleFavorite(id: string): Promise> { try { return await this.makeRequest>(`/flashcards/${id}/favorite`, { method: 'POST', }); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to toggle favorite', }; } } } export const flashcardsService = new FlashcardsService();