class Order {
  final int id;
  final String status;
  final String total;
  final String currency;
  final DateTime dateCreated;
  final List<OrderItem> items;
  final BillingAddress? billing;
  final ShippingAddress? shipping;
  final String? paymentMethod;
  final String? paymentMethodTitle;

  Order({
    required this.id,
    required this.status,
    required this.total,
    required this.currency,
    required this.dateCreated,
    required this.items,
    this.billing,
    this.shipping,
    this.paymentMethod,
    this.paymentMethodTitle,
  });

  factory Order.fromJson(Map<String, dynamic> json) {
    final items = (json['line_items'] as List<dynamic>?)
            ?.map((item) => OrderItem.fromJson(item))
            .toList() ??
        [];

    return Order(
      id: json['id'] ?? 0,
      status: json['status'] ?? '',
      total: json['total'] ?? '0',
      currency: json['currency'] ?? 'EUR',
      dateCreated: json['date_created'] != null
          ? DateTime.parse(json['date_created'])
          : DateTime.now(),
      items: items,
      billing: json['billing'] != null
          ? BillingAddress.fromJson(json['billing'])
          : null,
      shipping: json['shipping'] != null
          ? ShippingAddress.fromJson(json['shipping'])
          : null,
      paymentMethod: json['payment_method'],
      paymentMethodTitle: json['payment_method_title'],
    );
  }

  String get statusDisplay {
    switch (status) {
      case 'pending':
        return 'Ausstehend';
      case 'processing':
        return 'In Bearbeitung';
      case 'on-hold':
        return 'Wartend';
      case 'completed':
        return 'Abgeschlossen';
      case 'cancelled':
        return 'Storniert';
      case 'refunded':
        return 'Erstattet';
      case 'failed':
        return 'Fehlgeschlagen';
      default:
        return status;
    }
  }
}

class OrderItem {
  final int id;
  final String name;
  final int quantity;
  final String price;
  final String? imageUrl;

  OrderItem({
    required this.id,
    required this.name,
    required this.quantity,
    required this.price,
    this.imageUrl,
  });

  factory OrderItem.fromJson(Map<String, dynamic> json) {
    return OrderItem(
      id: json['id'] ?? 0,
      name: json['name'] ?? '',
      quantity: json['quantity'] ?? 0,
      price: json['price'] ?? '0',
      imageUrl: json['image']?['src'],
    );
  }
}

class BillingAddress {
  final String? firstName;
  final String? lastName;
  final String? company;
  final String? address1;
  final String? address2;
  final String? city;
  final String? state;
  final String? postcode;
  final String? country;
  final String? email;
  final String? phone;

  BillingAddress({
    this.firstName,
    this.lastName,
    this.company,
    this.address1,
    this.address2,
    this.city,
    this.state,
    this.postcode,
    this.country,
    this.email,
    this.phone,
  });

  factory BillingAddress.fromJson(Map<String, dynamic> json) {
    return BillingAddress(
      firstName: json['first_name'],
      lastName: json['last_name'],
      company: json['company'],
      address1: json['address_1'],
      address2: json['address_2'],
      city: json['city'],
      state: json['state'],
      postcode: json['postcode'],
      country: json['country'],
      email: json['email'],
      phone: json['phone'],
    );
  }

  String get fullAddress {
    final parts = <String>[];
    if (address1 != null && address1!.isNotEmpty) parts.add(address1!);
    if (address2 != null && address2!.isNotEmpty) parts.add(address2!);
    if (postcode != null && postcode!.isNotEmpty) parts.add(postcode!);
    if (city != null && city!.isNotEmpty) parts.add(city!);
    if (country != null && country!.isNotEmpty) parts.add(country!);
    return parts.join(', ');
  }
}

class ShippingAddress {
  final String? firstName;
  final String? lastName;
  final String? company;
  final String? address1;
  final String? address2;
  final String? city;
  final String? state;
  final String? postcode;
  final String? country;

  ShippingAddress({
    this.firstName,
    this.lastName,
    this.company,
    this.address1,
    this.address2,
    this.city,
    this.state,
    this.postcode,
    this.country,
  });

  factory ShippingAddress.fromJson(Map<String, dynamic> json) {
    return ShippingAddress(
      firstName: json['first_name'],
      lastName: json['last_name'],
      company: json['company'],
      address1: json['address_1'],
      address2: json['address_2'],
      city: json['city'],
      state: json['state'],
      postcode: json['postcode'],
      country: json['country'],
    );
  }

  String get fullAddress {
    final parts = <String>[];
    if (address1 != null && address1!.isNotEmpty) parts.add(address1!);
    if (address2 != null && address2!.isNotEmpty) parts.add(address2!);
    if (postcode != null && postcode!.isNotEmpty) parts.add(postcode!);
    if (city != null && city!.isNotEmpty) parts.add(city!);
    if (country != null && country!.isNotEmpty) parts.add(country!);
    return parts.join(', ');
  }
}

