/// Data Model: Review Response
///
/// Complete API response including reviews, statistics, and user feedback status.
library;

import 'package:worker/features/reviews/data/models/review_model.dart';
import 'package:worker/features/reviews/data/models/review_statistics_model.dart';

/// Review response data model
///
/// Wraps the complete API response structure:
/// ```json
/// {
///   "feedbacks": [...],
///   "is_already_feedback": false,
///   "my_feedback": {...} or null,
///   "statistics": {...}
/// }
/// ```
class ReviewResponseModel {
  const ReviewResponseModel({
    required this.feedbacks,
    required this.isAlreadyFeedback,
    required this.statistics,
    this.myFeedback,
  });

  /// List of all reviews/feedbacks
  final List<ReviewModel> feedbacks;

  /// Whether current user has already submitted feedback
  final bool isAlreadyFeedback;

  /// Current user's feedback (if exists)
  final ReviewModel? myFeedback;

  /// Aggregate statistics
  final ReviewStatisticsModel statistics;

  /// Create model from JSON
  factory ReviewResponseModel.fromJson(Map<String, dynamic> json) {
    final feedbacksList = json['feedbacks'] as List<dynamic>? ?? [];
    final feedbacks = feedbacksList
        .map((item) => ReviewModel.fromJson(item as Map<String, dynamic>))
        .toList();

    ReviewModel? myFeedback;
    if (json['my_feedback'] != null && json['my_feedback'] is Map<String, dynamic>) {
      myFeedback = ReviewModel.fromJson(json['my_feedback'] as Map<String, dynamic>);
    }

    final statistics = json['statistics'] != null && json['statistics'] is Map<String, dynamic>
        ? ReviewStatisticsModel.fromJson(json['statistics'] as Map<String, dynamic>)
        : const ReviewStatisticsModel(totalFeedback: 0, averageRating: 0.0);

    return ReviewResponseModel(
      feedbacks: feedbacks,
      isAlreadyFeedback: json['is_already_feedback'] as bool? ?? false,
      myFeedback: myFeedback,
      statistics: statistics,
    );
  }

  /// Convert model to JSON
  Map<String, dynamic> toJson() {
    return {
      'feedbacks': feedbacks.map((r) => r.toJson()).toList(),
      'is_already_feedback': isAlreadyFeedback,
      'my_feedback': myFeedback?.toJson(),
      'statistics': statistics.toJson(),
    };
  }

  @override
  String toString() {
    return 'ReviewResponseModel(feedbacks: ${feedbacks.length}, '
        'isAlreadyFeedback: $isAlreadyFeedback, statistics: $statistics)';
  }
}
