import 'word_model.dart';

/// 生词本模型
class WordBook {
  final String id;
  final String name;
  final String? description;
  final String userId;
  final List<String> wordIds;
  final List<Word> words;
  final WordBookType type;
  final int wordCount;
  final DateTime createdAt;
  final DateTime updatedAt;

  WordBook({
    required this.id,
    required this.name,
    this.description,
    required this.userId,
    List<String>? wordIds,
    List<Word>? words,
    this.type = WordBookType.personal,
    this.wordCount = 0,
    required this.createdAt,
    required this.updatedAt,
  })  : wordIds = wordIds ?? [],
        words = words ?? [];

  factory WordBook.fromJson(Map<String, dynamic> json) {
    return WordBook(
      id: json['id'] as String,
      name: json['name'] as String,
      description: json['description'] as String?,
      userId: json['user_id'] as String,
      wordIds: (json['word_ids'] as List<dynamic>?)?.map((e) => e as String).toList(),
      words: (json['words'] as List<dynamic>?)?.map((e) => Word.fromJson(e as Map<String, dynamic>)).toList(),
      type: WordBookType.values.firstWhere(
        (e) => e.toString().split('.').last == json['type'],
        orElse: () => WordBookType.personal,
      ),
      wordCount: json['word_count'] as int? ?? 0,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'user_id': userId,
      'word_ids': wordIds,
      'words': words.map((e) => e.toJson()).toList(),
      'type': type.toString().split('.').last,
      'word_count': wordCount,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  WordBook copyWith({
    String? id,
    String? name,
    String? description,
    String? userId,
    List<String>? wordIds,
    List<Word>? words,
    WordBookType? type,
    int? wordCount,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return WordBook(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      userId: userId ?? this.userId,
      wordIds: wordIds ?? this.wordIds,
      words: words ?? this.words,
      type: type ?? this.type,
      wordCount: wordCount ?? this.wordCount,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
}

/// 生词本类型
enum WordBookType {
  personal,
  shared,
  system,
}

/// 生词本条目
class WordBookEntry {
  final String id;
  final String wordBookId;
  final String wordId;
  final Word word;
  final String? note;
  final List<String> tags;
  final int reviewCount;
  final int correctCount;
  final DateTime? lastReviewAt;
  final DateTime addedAt;

  WordBookEntry({
    required this.id,
    required this.wordBookId,
    required this.wordId,
    required this.word,
    this.note,
    List<String>? tags,
    this.reviewCount = 0,
    this.correctCount = 0,
    this.lastReviewAt,
    required this.addedAt,
  }) : tags = tags ?? [];

  factory WordBookEntry.fromJson(Map<String, dynamic> json) {
    return WordBookEntry(
      id: json['id'] as String,
      wordBookId: json['word_book_id'] as String,
      wordId: json['word_id'] as String,
      word: Word.fromJson(json['word'] as Map<String, dynamic>),
      note: json['note'] as String?,
      tags: (json['tags'] as List<dynamic>?)?.map((e) => e as String).toList(),
      reviewCount: json['review_count'] as int? ?? 0,
      correctCount: json['correct_count'] as int? ?? 0,
      lastReviewAt: json['last_review_at'] != null ? DateTime.parse(json['last_review_at'] as String) : null,
      addedAt: DateTime.parse(json['added_at'] as String),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'word_book_id': wordBookId,
      'word_id': wordId,
      'word': word.toJson(),
      'note': note,
      'tags': tags,
      'review_count': reviewCount,
      'correct_count': correctCount,
      'last_review_at': lastReviewAt?.toIso8601String(),
      'added_at': addedAt.toIso8601String(),
    };
  }
}

/// 生词本统计
class WordBookStats {
  final String wordBookId;
  final int totalWords;
  final int masteredWords;
  final int reviewingWords;
  final int newWords;
  final double masteryRate;
  final DateTime? lastStudyAt;
  final int studyDays;
  final int totalReviews;

  WordBookStats({
    required this.wordBookId,
    this.totalWords = 0,
    this.masteredWords = 0,
    this.reviewingWords = 0,
    this.newWords = 0,
    this.masteryRate = 0,
    this.lastStudyAt,
    this.studyDays = 0,
    this.totalReviews = 0,
  });

  factory WordBookStats.fromJson(Map<String, dynamic> json) {
    return WordBookStats(
      wordBookId: json['word_book_id'] as String,
      totalWords: json['total_words'] as int? ?? 0,
      masteredWords: json['mastered_words'] as int? ?? 0,
      reviewingWords: json['reviewing_words'] as int? ?? 0,
      newWords: json['new_words'] as int? ?? 0,
      masteryRate: (json['mastery_rate'] as num?)?.toDouble() ?? 0,
      lastStudyAt: json['last_study_at'] != null ? DateTime.parse(json['last_study_at'] as String) : null,
      studyDays: json['study_days'] as int? ?? 0,
      totalReviews: json['total_reviews'] as int? ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'word_book_id': wordBookId,
      'total_words': totalWords,
      'mastered_words': masteredWords,
      'reviewing_words': reviewingWords,
      'new_words': newWords,
      'mastery_rate': masteryRate,
      'last_study_at': lastStudyAt?.toIso8601String(),
      'study_days': studyDays,
      'total_reviews': totalReviews,
    };
  }
}