// 簡化的 Flashcards API service - 移除 CardSets 概念

export interface SimpleFlashcard {
  id: string;
  word: string;
  translation: string;
  definition: string;
  partOfSpeech: string;
  pronunciation: string;
  example: string;
  exampleTranslation?: string;
  masteryLevel: number;
  timesReviewed: number;
  isFavorite: boolean;
  nextReviewDate: string;
  difficultyLevel: string;
  createdAt: string;
  updatedAt?: string; // 設為可選，因為模擬資料可能沒有
  // 移除 cardSet 屬性
}

export interface CreateSimpleFlashcardRequest {
  word: string;
  translation: string;
  definition: string;
  pronunciation: string;
  partOfSpeech: string;
  example: string;
  exampleTranslation?: string;
  // 移除 cardSetId
}

export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
  message?: string;
}

class SimplifiedFlashcardsService {
  private readonly baseURL = `${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5008'}/api`;

  private async makeRequest<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
    const response = await fetch(`${this.baseURL}${endpoint}`, {
      headers: {
        'Content-Type': 'application/json',
        ...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();
  }

  // 簡化的詞卡方法
  async getFlashcards(search?: string, favoritesOnly: boolean = false): Promise<ApiResponse<{ flashcards: SimpleFlashcard[], count: number }>> {
    try {
      const params = new URLSearchParams();
      if (search) params.append('search', search);
      if (favoritesOnly) params.append('favoritesOnly', 'true');

      const queryString = params.toString();
      const endpoint = `/flashcards-simple${queryString ? `?${queryString}` : ''}`;

      return await this.makeRequest<ApiResponse<{ flashcards: SimpleFlashcard[], count: number }>>(endpoint);
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to fetch flashcards',
      };
    }
  }

  async createFlashcard(data: CreateSimpleFlashcardRequest): Promise<ApiResponse<SimpleFlashcard>> {
    try {
      return await this.makeRequest<ApiResponse<SimpleFlashcard>>('/flashcards-simple', {
        method: 'POST',
        body: JSON.stringify(data),
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to create flashcard',
      };
    }
  }

  async deleteFlashcard(id: string): Promise<ApiResponse<void>> {
    try {
      return await this.makeRequest<ApiResponse<void>>(`/flashcards-simple/${id}`, {
        method: 'DELETE',
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to delete flashcard',
      };
    }
  }

  async toggleFavorite(id: string): Promise<ApiResponse<void>> {
    try {
      return await this.makeRequest<ApiResponse<void>>(`/flashcards-simple/${id}/favorite`, {
        method: 'POST',
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to toggle favorite',
      };
    }
  }
}

export const simplifiedFlashcardsService = new SimplifiedFlashcardsService();