// Flashcards API service for handling flashcard operations

export interface CardSet {
  id: string;
  name: string;
  description: string;
  color: string;
  cardCount: number;
  createdAt: string;
  updatedAt: string;
  isDefault: boolean;
  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;
  word: string;
  translation: string;
  definition: string;
  pronunciation: string;
  partOfSpeech: string;
  example: string;
}

export interface ApiResponse<T> {
  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<T>(
    endpoint: string,
    options: RequestInit = {}
  ): Promise<T> {
    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<ApiResponse<{ sets: CardSet[] }>> {
    try {
      return await this.makeRequest<ApiResponse<{ sets: CardSet[] }>>('/cardsets');
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to fetch card sets',
      };
    }
  }

  async createCardSet(data: CreateCardSetRequest): Promise<ApiResponse<CardSet>> {
    try {
      return await this.makeRequest<ApiResponse<CardSet>>('/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<ApiResponse<void>> {
    try {
      return await this.makeRequest<ApiResponse<void>>(`/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<ApiResponse<{ flashcards: Flashcard[]; total: number; hasMore: boolean }>> {
    try {
      const query = cardSetId ? `?cardSetId=${cardSetId}` : '';
      return await this.makeRequest<ApiResponse<{ flashcards: Flashcard[]; total: number; hasMore: boolean }>>(`/flashcards${query}`);
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to fetch flashcards',
      };
    }
  }

  async createFlashcard(data: CreateFlashcardRequest): Promise<ApiResponse<Flashcard>> {
    try {
      return await this.makeRequest<ApiResponse<Flashcard>>('/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<CreateFlashcardRequest>): Promise<ApiResponse<Flashcard>> {
    try {
      return await this.makeRequest<ApiResponse<Flashcard>>(`/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<ApiResponse<void>> {
    try {
      return await this.makeRequest<ApiResponse<void>>(`/flashcards/${id}`, {
        method: 'DELETE',
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to delete flashcard',
      };
    }
  }

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

  async ensureDefaultCardSet(): Promise<ApiResponse<CardSet>> {
    try {
      return await this.makeRequest<ApiResponse<CardSet>>('/cardsets/ensure-default', {
        method: 'POST',
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to ensure default card set',
      };
    }
  }

  async batchCreateFlashcards(request: BatchCreateFlashcardsRequest): Promise<ApiResponse<BatchCreateFlashcardsResponse>> {
    try {
      return await this.makeRequest<ApiResponse<BatchCreateFlashcardsResponse>>('/flashcards/batch', {
        method: 'POST',
        body: JSON.stringify(request),
      });
    } catch (error) {
      return {
        success: false,
        error: error instanceof Error ? error.message : 'Failed to save flashcards',
      };
    }
  }
}

// 新增批量創建相關介面
export interface BatchCreateFlashcardsRequest {
  cardSetId?: string;
  cards: CreateFlashcardRequest[];
}

export interface BatchCreateFlashcardsResponse {
  savedCards: SavedCard[];
  savedCount: number;
  errorCount: number;
  errors: string[];
}

export interface SavedCard {
  id: string;
  word: string;
  translation: string;
}

export const flashcardsService = new FlashcardsService();