import '../api/types.dart';
import 'delivery_contact.dart';

class DeliveryOrder implements Serializable {
  final int id;
  final bool isNewCustomer;
  final String? note;
  final double totalAmount;
  final double? totalPaid;
  final int? totalItems;
  final List<DeliveryContact> contacts;
  final DeliveryContact? contact;

  const DeliveryOrder({
    required this.id,
    required this.isNewCustomer,
    this.note,
    required this.totalAmount,
    this.totalPaid,
    this.totalItems,
    required this.contacts,
    this.contact,
  });

  factory DeliveryOrder.fromJson(Map<String, dynamic> json) {
    return DeliveryOrder(
      id: json['id'] as int,
      isNewCustomer: json['isNewCustomer'] as bool,
      note: json['note'] as String?,
      totalAmount: (json['totalAmount'] as num).toDouble(),
      totalPaid: (json['totalPaid'] as num?)?.toDouble(),
      totalItems: json['totalItems'] as int?,
      contacts: (json['contacts'] as List?)
          ?.map((e) => DeliveryContact.fromJson(e as Map<String, dynamic>))
          .toList() ?? [],
      contact: json['contact'] != null
          ? DeliveryContact.fromJson(json['contact'] as Map<String, dynamic>)
          : null,
    );
  }

  @override
  Map<String, Object?> toJson() => {
    'id': id,
    'isNewCustomer': isNewCustomer,
    'note': note,
    'totalAmount': totalAmount,
    'totalPaid': totalPaid,
    'totalItems': totalItems,
    'contacts': contacts.map((c) => c.toJson()).toList(),
    'contact': contact?.toJson(),
  };
}
