import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_client.dart';

/// 生词本服务
class WordBookService {
  final ApiClient _apiClient = ApiClient.instance;

  WordBookService();

  /// 切换单词收藏状态
  Future<Map<String, dynamic>> toggleFavorite(int wordId) async {
    final response = await _apiClient.post('/word-book/toggle/$wordId');
    return response.data['data'];
  }

  /// 获取生词本列表
  Future<Map<String, dynamic>> getFavoriteWords({
    int page = 1,
    int pageSize = 20,
    String sortBy = 'created_at',
    String order = 'desc',
  }) async {
    final response = await _apiClient.get(
      '/word-book',
      queryParameters: {
        'page': page,
        'page_size': pageSize,
        'sort_by': sortBy,
        'order': order,
      },
    );
    return response.data['data'];
  }

  /// 获取指定词汇书的生词本
  Future<List<dynamic>> getFavoriteWordsByBook(String bookId) async {
    final response = await _apiClient.get('/word-book/books/$bookId');
    return response.data['data']['words'];
  }

  /// 获取生词本统计信息
  Future<Map<String, dynamic>> getFavoriteStats() async {
    final response = await _apiClient.get('/word-book/stats');
    return response.data['data'];
  }

  /// 批量添加到生词本
  Future<int> batchAddToFavorite(List<int> wordIds) async {
    final response = await _apiClient.post(
      '/word-book/batch',
      data: {'word_ids': wordIds},
    );
    return response.data['data']['added_count'];
  }

  /// 从生词本移除单词
  Future<void> removeFromFavorite(int wordId) async {
    await _apiClient.delete('/word-book/$wordId');
  }
}

/// 生词本服务提供者
final wordBookServiceProvider = Provider<WordBookService>((ref) {
  return WordBookService();
});
