import 'flyer_import_item.dart';

class FlyerImportResult {
  final int? sessionId;
  final List<FlyerImportItem> items;
  final List<String> warnings;
  final bool sourceAvailable;
  final String? sourceFileName;
  final String? sourceMimeType;
  final int? sourceFileSize;

  FlyerImportResult({
    required this.sessionId,
    required this.items,
    required this.warnings,
    required this.sourceAvailable,
    this.sourceFileName,
    this.sourceMimeType,
    this.sourceFileSize,
  });

  factory FlyerImportResult.fromJson(Map<String, dynamic> json) {
    final rawItems = (json['items'] as List?) ?? const [];
    final warnings = (json['warnings'] as List?)
            ?.map((warning) => warning.toString())
            .toList() ??
        const <String>[];

    return FlyerImportResult(
      sessionId: (json['sessionId'] as num?)?.toInt(),
      items: rawItems
          .map((item) => FlyerImportItem.fromJson(item as Map<String, dynamic>))
          .toList(),
      warnings: warnings,
      sourceAvailable: json['sourceAvailable'] == true,
      sourceFileName: json['sourceFileName'] as String?,
      sourceMimeType: json['sourceMimeType'] as String?,
      sourceFileSize: (json['sourceFileSize'] as num?)?.toInt(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'sessionId': sessionId,
      'items': items.map((item) => item.toJson()).toList(),
      'warnings': warnings,
      'sourceAvailable': sourceAvailable,
      'sourceFileName': sourceFileName,
      'sourceMimeType': sourceMimeType,
      'sourceFileSize': sourceFileSize,
    };
  }
}
