import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
class TrackSummary {
  @JsonKey(name: 'text')
  String text;

  @JsonKey(name: 'items', includeToJson: false)
  Map<String, String>? items;

  TrackSummary({
    required this.text,
    this.items,
  });

  factory TrackSummary.fromJson(Map<String, dynamic> json) {
    final rawItems = json['items'] as List<dynamic>? ?? [];

    final parsedItems = <String, String>{};
    for (final item in rawItems) {
      if (item is Map<String, dynamic>) {
        final key = item['key']?.toString();
        final value = item['value']?.toString();
        if (key != null && value != null) {
          parsedItems[key] = value;
        }
      }
    }

    return TrackSummary(
      text: json['text'] ?? '',
      items: parsedItems,
    );
  }
}
