import 'order_item.dart';
import 'order_status.dart';

/// A customer order placed through the Kell Creations store.
class Order {
  final String id;
  final String customerName;
  final String customerEmail;

  /// Optional customer phone number.
  final String? customerPhone;

  final DateTime orderDate;
  final OrderStatus status;
  final List<OrderItem> items;
  final String shippingAddress;

  /// Optional internal operator notes about this order.
  final String? notes;

  /// Customer-visible order notes (e.g. gift message, special instructions).
  final List<String> orderNotes;

  const Order({
    required this.id,
    required this.customerName,
    required this.customerEmail,
    this.customerPhone,
    required this.orderDate,
    required this.status,
    required this.items,
    required this.shippingAddress,
    this.notes,
    this.orderNotes = const [],
  });

  /// The total value of the order.
  double get total => items.fold(0, (sum, item) => sum + item.lineTotal);

  /// The number of individual items in the order.
  int get itemCount => items.fold(0, (sum, item) => sum + item.quantity);

  /// Returns a copy of this order with the given fields replaced.
  Order copyWith({
    String? id,
    String? customerName,
    String? customerEmail,
    String? customerPhone,
    DateTime? orderDate,
    OrderStatus? status,
    List<OrderItem>? items,
    String? shippingAddress,
    String? notes,
    List<String>? orderNotes,
  }) {
    return Order(
      id: id ?? this.id,
      customerName: customerName ?? this.customerName,
      customerEmail: customerEmail ?? this.customerEmail,
      customerPhone: customerPhone ?? this.customerPhone,
      orderDate: orderDate ?? this.orderDate,
      status: status ?? this.status,
      items: items ?? this.items,
      shippingAddress: shippingAddress ?? this.shippingAddress,
      notes: notes ?? this.notes,
      orderNotes: orderNotes ?? this.orderNotes,
    );
  }
}
