// lib/data/models/comment_model.dart

import 'package:equatable/equatable.dart';

class CommentModel extends Equatable {
  final String id;
  final String userName;
  final double rating;
  final String comment;
  final DateTime publishedAt;
  final List<String> uploadedImageUrls;

  const CommentModel({
    required this.id,
    required this.userName,
    required this.rating,
    required this.comment,
    required this.publishedAt,
    this.uploadedImageUrls = const [],
  });

  @override
  List<Object?> get props => [id, userName, rating, comment, publishedAt, uploadedImageUrls];

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

    return CommentModel(
      id: json['ID'] ?? '',
      userName: json['User']?['Name'] ?? 'کاربر ناشناس',
      rating: (json['Score'] as num?)?.toDouble() ?? 0.0,
      comment: json['Text'] ?? '',
      publishedAt: DateTime.tryParse(json['createdAt'] ?? '') ?? DateTime.now(),
      uploadedImageUrls: images,
    );
  }
}