import 'package:freezed_annotation/freezed_annotation.dart';
import 'vocabulary_book_model.dart';

part 'study_plan_model.freezed.dart';
part 'study_plan_model.g.dart';

/// 学习计划模型
@freezed
class StudyPlan with _$StudyPlan {
  const factory StudyPlan({
    required String id,
    required String name,
    String? description,
    required String userId,
    required StudyPlanType type,
    required StudyPlanStatus status,
    required DateTime startDate,
    required DateTime endDate,
    required int dailyTarget,
    @Default([]) List<String> vocabularyBookIds,
    @Default([]) List<VocabularyBook> vocabularyBooks,
    @Default(0) int totalWords,
    @Default(0) int completedWords,
    @Default(0) int currentStreak,
    @Default(0) int maxStreak,
    @Default([]) List<StudyPlanMilestone> milestones,
    @Default({}) Map<String, int> dailyProgress,
    required DateTime createdAt,
    required DateTime updatedAt,
  }) = _StudyPlan;

  factory StudyPlan.fromJson(Map<String, dynamic> json) => _$StudyPlanFromJson(json);
}

/// 学习计划类型
enum StudyPlanType {
  @JsonValue('daily')
  daily,
  @JsonValue('weekly')
  weekly,
  @JsonValue('monthly')
  monthly,
  @JsonValue('custom')
  custom,
  @JsonValue('exam_prep')
  examPrep,
}

/// 学习计划状态
enum StudyPlanStatus {
  @JsonValue('active')
  active,
  @JsonValue('paused')
  paused,
  @JsonValue('completed')
  completed,
  @JsonValue('cancelled')
  cancelled,
}

/// 学习计划里程碑
@freezed
class StudyPlanMilestone with _$StudyPlanMilestone {
  const factory StudyPlanMilestone({
    required String id,
    required String title,
    String? description,
    required int targetWords,
    required DateTime targetDate,
    @Default(false) bool isCompleted,
    DateTime? completedAt,
    @Default(0) int currentProgress,
  }) = _StudyPlanMilestone;

  factory StudyPlanMilestone.fromJson(Map<String, dynamic> json) => _$StudyPlanMilestoneFromJson(json);
}

/// 学习计划统计
@freezed
class StudyPlanStats with _$StudyPlanStats {
  const factory StudyPlanStats({
    required String planId,
    @Default(0) int totalDays,
    @Default(0) int studiedDays,
    @Default(0) int totalWords,
    @Default(0) int learnedWords,
    @Default(0) int reviewedWords,
    @Default(0) int currentStreak,
    @Default(0) int maxStreak,
    @Default(0.0) double completionRate,
    @Default(0.0) double averageDailyWords,
    @Default(0) int remainingDays,
    @Default(0) int remainingWords,
    DateTime? lastStudyDate,
    @Default([]) List<DailyStudyRecord> dailyRecords,
  }) = _StudyPlanStats;

  factory StudyPlanStats.fromJson(Map<String, dynamic> json) => _$StudyPlanStatsFromJson(json);
}

/// 每日学习记录
@freezed
class DailyStudyRecord with _$DailyStudyRecord {
  const factory DailyStudyRecord({
    required DateTime date,
    @Default(0) int wordsLearned,
    @Default(0) int wordsReviewed,
    @Default(0) int studyTimeMinutes,
    @Default(false) bool targetAchieved,
    @Default([]) List<String> vocabularyBookIds,
  }) = _DailyStudyRecord;

  factory DailyStudyRecord.fromJson(Map<String, dynamic> json) => _$DailyStudyRecordFromJson(json);
}

/// 学习计划模板
@freezed
class StudyPlanTemplate with _$StudyPlanTemplate {
  const factory StudyPlanTemplate({
    required String id,
    required String name,
    required String description,
    required StudyPlanType type,
    required int durationDays,
    required int dailyTarget,
    @Default([]) List<String> recommendedBookIds,
    @Default([]) List<StudyPlanMilestone> milestones,
    @Default(0) int difficulty,
    @Default([]) List<String> tags,
    @Default(false) bool isPopular,
  }) = _StudyPlanTemplate;

  factory StudyPlanTemplate.fromJson(Map<String, dynamic> json) => _$StudyPlanTemplateFromJson(json);
}