/// 写作完成记录模型
class WritingRecord {
  final String id;
  final String taskId;
  final String taskTitle;
  final String taskDescription;
  final String content;
  final int wordCount;
  final int timeUsed; // 使用的时间（秒）
  final int score;
  final DateTime completedAt;
  final Map<String, dynamic>? feedback;

  const WritingRecord({
    required this.id,
    required this.taskId,
    required this.taskTitle,
    required this.taskDescription,
    required this.content,
    required this.wordCount,
    required this.timeUsed,
    required this.score,
    required this.completedAt,
    this.feedback,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'taskId': taskId,
      'taskTitle': taskTitle,
      'taskDescription': taskDescription,
      'content': content,
      'wordCount': wordCount,
      'timeUsed': timeUsed,
      'score': score,
      'completedAt': completedAt.toIso8601String(),
      'feedback': feedback,
    };
  }

  factory WritingRecord.fromJson(Map<String, dynamic> json) {
    return WritingRecord(
      id: json['id'],
      taskId: json['taskId'],
      taskTitle: json['taskTitle'],
      taskDescription: json['taskDescription'],
      content: json['content'],
      wordCount: json['wordCount'],
      timeUsed: json['timeUsed'],
      score: json['score'],
      completedAt: DateTime.parse(json['completedAt']),
      feedback: json['feedback'],
    );
  }

  WritingRecord copyWith({
    String? id,
    String? taskId,
    String? taskTitle,
    String? taskDescription,
    String? content,
    int? wordCount,
    int? timeUsed,
    int? score,
    DateTime? completedAt,
    Map<String, dynamic>? feedback,
  }) {
    return WritingRecord(
      id: id ?? this.id,
      taskId: taskId ?? this.taskId,
      taskTitle: taskTitle ?? this.taskTitle,
      taskDescription: taskDescription ?? this.taskDescription,
      content: content ?? this.content,
      wordCount: wordCount ?? this.wordCount,
      timeUsed: timeUsed ?? this.timeUsed,
      score: score ?? this.score,
      completedAt: completedAt ?? this.completedAt,
      feedback: feedback ?? this.feedback,
    );
  }

  String get formattedDate {
    return '${completedAt.year}-${completedAt.month.toString().padLeft(2, '0')}-${completedAt.day.toString().padLeft(2, '0')}';
  }

  String get formattedTime {
    final minutes = timeUsed ~/ 60;
    final seconds = timeUsed % 60;
    return '${minutes}分${seconds}秒';
  }

  String get scoreText {
    return '${score}分';
  }
}