import 'package:persian_datetime_picker/persian_datetime_picker.dart';

class CommentEntity {
  final String id;
  final String userFullName;
  final String? userImage;
  final String comment;
  final double score;
  final DateTime createdAt;
  final List<String> uploadedImageUrls;

  CommentEntity({
    required this.id,
    required this.userFullName,
    this.userImage,
    required this.comment,
    required this.score,
    required this.createdAt,
    this.uploadedImageUrls = const [],
  });

  factory CommentEntity.fromJson(Map<String, dynamic> json) {
    final user = json['User'] as Map<String, dynamic>? ?? {};
    
    final userImages = json['UserImages'] as List<dynamic>? ?? [];
    final List<String> images = userImages
        .map((image) => (image as Map<String, dynamic>)['Url'] as String)
        .toList();

    return CommentEntity(
      id: json['ID'] as String? ?? '',
      userFullName: user['Name'] as String? ?? 'کاربر مهمان',
      userImage: user['Image'] as String?,
      comment: json['Text'] as String? ?? 'بدون نظر',
      score: (json['Score'] as num? ?? 0).toDouble(),
      createdAt: DateTime.tryParse(json['createdAt'] as String? ?? '') ?? DateTime.now(),
      uploadedImageUrls: images,
    );
  }
  
  String get persianDate {
    final jalali = Jalali.fromDateTime(createdAt);
    return jalali.formatFullDate();
  }
}