import '../../../models/entities/review.dart';
import '../../common/extensions/extensions.dart';
import '../../models/entities/review_payload.dart';

extension ReviewParser on Review {
  static Review fromJudge(Map<String, dynamic> parsedJson) {
    final reviewer = parsedJson['reviewer'];
    final listImageData = List.from(parsedJson['pictures']);
    var images = <String>[];
    for (var item in listImageData) {
      if (item is Map && item['hidden'] == false) {
        final image = item['urls']?['huge']?.toString();
        if (image is String && image.isURL) {
          images.add(image);
        }
      }
    }
    return Review(
      id: parsedJson['id'],
      title: parsedJson['title'],
      review: parsedJson['body'],
      rating: double.parse(parsedJson['rating'].toString()),
      createdAt: parsedJson['created_at'] != null
          ? DateTime.parse(parsedJson['created_at'])
          : DateTime.now(),
      name: reviewer['name'],
      email: reviewer['email'],
      // avatar: parsedJson['avatar'],
      // status: parsedJson['status'],
      // isApproved: parsedJson['is_approved'],
      images: images,
    );
  }
}

extension JudgeProductExt on String {
  // parse gid://shopify/Product/1234567890 to 1234567890
  String get numberOfProductId => split('/').last;
}

extension JudgeReviewPayloadExt on ReviewPayload {
  Map<String, dynamic> toJudgeJson() {
    return {
      'rating': rating,
      'id': productId.numberOfProductId,
      'email': reviewerEmail,
      'name': reviewerName,
      'body': review,
      'title': title,
      'shop_domain': shopDomain,
      'reviewer_name_format': '',
    };
  }
}
