import 'course.dart';

/// Result of a single course evaluation
class EvaluationResult {
  final Course course;
  final bool success;
  final String? errorMessage;
  final DateTime timestamp;

  EvaluationResult({
    required this.course,
    required this.success,
    this.errorMessage,
    DateTime? timestamp,
  }) : timestamp = timestamp ?? DateTime.now();

  factory EvaluationResult.fromJson(Map<String, dynamic> json) {
    return EvaluationResult(
      course: Course.fromJson(json['course'] as Map<String, dynamic>),
      success: json['success'] as bool? ?? false,
      errorMessage: json['errorMessage'] as String?,
      timestamp: json['timestamp'] != null
          ? DateTime.parse(json['timestamp'] as String)
          : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'course': course.toJson(),
      'success': success,
      'errorMessage': errorMessage,
      'timestamp': timestamp.toIso8601String(),
    };
  }

  @override
  String toString() {
    return 'EvaluationResult(course: ${course.name}, success: $success, errorMessage: $errorMessage)';
  }
}

/// Result of batch evaluation
class BatchEvaluationResult {
  final int total;
  final int success;
  final int failed;
  final List<EvaluationResult> results;
  final Duration duration;

  BatchEvaluationResult({
    required this.total,
    required this.success,
    required this.failed,
    required this.results,
    required this.duration,
  });

  factory BatchEvaluationResult.fromJson(Map<String, dynamic> json) {
    return BatchEvaluationResult(
      total: json['total'] as int? ?? 0,
      success: json['success'] as int? ?? 0,
      failed: json['failed'] as int? ?? 0,
      results:
          (json['results'] as List<dynamic>?)
              ?.map((e) => EvaluationResult.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      duration: Duration(milliseconds: json['durationMs'] as int? ?? 0),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'success': success,
      'failed': failed,
      'results': results.map((e) => e.toJson()).toList(),
      'durationMs': duration.inMilliseconds,
    };
  }

  double get successRate => total > 0 ? success / total : 0.0;

  @override
  String toString() {
    return 'BatchEvaluationResult(total: $total, success: $success, failed: $failed, duration: ${duration.inSeconds}s)';
  }
}
