import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../models/writing_task.dart';
import '../models/writing_submission.dart';
import '../services/writing_service.dart';

/// 写作任务状态
class WritingTasksState {
  final List<WritingTask> tasks;
  final bool isLoading;
  final String? error;

  WritingTasksState({
    this.tasks = const [],
    this.isLoading = false,
    this.error,
  });

  WritingTasksState copyWith({
    List<WritingTask>? tasks,
    bool? isLoading,
    String? error,
  }) {
    return WritingTasksState(
      tasks: tasks ?? this.tasks,
      isLoading: isLoading ?? this.isLoading,
      error: error,
    );
  }
}

/// 写作任务 Notifier
class WritingTasksNotifier extends StateNotifier<WritingTasksState> {
  final WritingService _writingService;

  WritingTasksNotifier(this._writingService) : super(WritingTasksState());

  /// 加载写作任务列表
  Future<void> loadTasks({
    WritingType? type,
    WritingDifficulty? difficulty,
    int page = 1,
    bool forceRefresh = false,
  }) async {
    state = state.copyWith(isLoading: true, error: null);
    
    try {
      final tasks = await _writingService.getWritingTasks(
        type: type,
        difficulty: difficulty,
        page: page,
        forceRefresh: forceRefresh,
      );
      
      state = state.copyWith(tasks: tasks, isLoading: false);
    } catch (e) {
      state = state.copyWith(
        isLoading: false,
        error: e.toString(),
      );
    }
  }

  /// 获取推荐任务
  Future<void> loadRecommendedTasks(String userId, {bool forceRefresh = false}) async {
    state = state.copyWith(isLoading: true, error: null);
    
    try {
      final tasks = await _writingService.getRecommendedWritingTasks(
        userId: userId,
        limit: 5,
        forceRefresh: forceRefresh,
      );
      
      state = state.copyWith(tasks: tasks, isLoading: false);
    } catch (e) {
      state = state.copyWith(
        isLoading: false,
        error: e.toString(),
      );
    }
  }
}

/// 写作服务 Provider
final writingServiceProvider = Provider<WritingService>((ref) {
  return WritingService();
});

/// 写作任务列表 Provider
final writingTasksProvider = StateNotifierProvider<WritingTasksNotifier, WritingTasksState>((ref) {
  final service = ref.watch(writingServiceProvider);
  return WritingTasksNotifier(service);
});

/// 按考试类型获取写作任务
final examWritingTasksProvider = FutureProvider.family<List<WritingTask>, ExamType>((ref, examType) async {
  final service = ref.watch(writingServiceProvider);
  
  try {
    // 从后端获取所有任务，然后在前端过滤
    final tasks = await service.getWritingTasks(limit: 100);
    return tasks.where((task) => task.examType == examType).toList();
  } catch (e) {
    return [];
  }
});

/// 用户写作历史 Provider
final userWritingHistoryProvider = FutureProvider.family<List<WritingSubmission>, String>((ref, userId) async {
  final service = ref.watch(writingServiceProvider);
  
  try {
    return await service.getUserWritingHistory(userId: userId, limit: 3);
  } catch (e) {
    return [];
  }
});
