import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/enums/delivery_type.dart';
import 'package:niogu_ecommerce_v1/core/enums/order_status.dart';
import 'package:niogu_ecommerce_v1/features/checkout/data/models/checkout_model.dart';

class OnlineOrder {
  final String outletId;

  final String outletNameSnapshot;

  final String? outletPhoneNumberSnapshot;

  final OutletAddressSnapshot? outletAddressSnapshot;

  final String customerId;

  final String customerNameSnapshot;

  final String? customerEmailSnapshot;

  final String customerPhoneNumberSnapshot;

  final CustomerAddressSnapshot? customerAddressSnapshot;

  final OrderStatus orderStatus;

  final DeliveryType deliveryType;

  final String deliveryPreference;

  final String? deliveryFeeType;

  final double deliveryFee;

  final double totalDeliveryFee;

  final double totalOrder;

  final double totalAmount;

  final String? notes;

  final MultipartFile? paymentProofFile;

  final String paymentMethod;

  final List<OnlineOrderItem> items;

  const OnlineOrder({
    required this.outletId,
    required this.outletNameSnapshot,
    required this.outletPhoneNumberSnapshot,
    required this.outletAddressSnapshot,
    required this.customerId,
    required this.customerNameSnapshot,
    this.customerEmailSnapshot,
    required this.customerPhoneNumberSnapshot,
    required this.orderStatus,
    required this.deliveryType,
    required this.deliveryPreference,
    this.deliveryFeeType,
    required this.deliveryFee,
    required this.totalDeliveryFee,
    required this.totalOrder,
    required this.totalAmount,
    this.notes,
    this.paymentProofFile,
    required this.paymentMethod,
    required this.customerAddressSnapshot,
    required this.items,
  });

  OnlineOrderRequest toOnlineOrderRequest() {
    return OnlineOrderRequest(
      outletId: outletId,
      outletNameSnapshot: outletNameSnapshot,
      outletPhoneNumberSnapshot: outletPhoneNumberSnapshot,
      outletAddressSnapshot: outletAddressSnapshot
          ?.toOutletAddressSnapshotRequest(),
      customerId: customerId,
      customerNameSnapshot: customerNameSnapshot,
      customerEmailSnapshot: customerEmailSnapshot,
      customerPhoneNumberSnapshot: customerPhoneNumberSnapshot,
      customerAddressSnapshot: customerAddressSnapshot
          ?.toCustomerAddressSnapshotRequest(),
      orderStatus: orderStatus.status,
      deliveryType: deliveryType.type,
      deliveryPreference: deliveryPreference,
      deliveryFeeType: deliveryFeeType,
      deliveryFee: deliveryFee,
      totalDeliveryFee: totalDeliveryFee,
      totalOrder: totalOrder,
      totalAmount: totalAmount,
      notes: notes,
      paymentProofFile: paymentProofFile,
      paymentMethod: paymentMethod,
      items: items.map((item) => item.toOnlineOrderItemRequest()).toList(),
    );
  }
}

class OutletAddressSnapshot {
  final String fullAddress;

  final double latitude;

  final double longitude;

  const OutletAddressSnapshot({
    required this.fullAddress,
    required this.latitude,
    required this.longitude,
  });

  OutletAddressSnapshotRequest toOutletAddressSnapshotRequest() {
    return OutletAddressSnapshotRequest(
      fullAddress: fullAddress,
      latitude: latitude,
      longitude: longitude,
    );
  }
}

class CustomerAddressSnapshot {
  final String label;

  final String fullAddress;

  final double latitude;

  final double longitude;

  const CustomerAddressSnapshot({
    required this.label,
    required this.fullAddress,
    required this.latitude,
    required this.longitude,
  });

  CustomerAddressSnapshotRequest toCustomerAddressSnapshotRequest() {
    return CustomerAddressSnapshotRequest(
      label: label,
      fullAddress: fullAddress,
      latitude: latitude,
      longitude: longitude,
    );
  }
}

class OnlineOrderItem {
  final String productVariantId;

  final int quantity;

  final String? productImageUrlSnapshot;

  final String productNameSnapshot;

  final String? productVariantNameSnapshot;

  final double sellingPriceSnapshot;

  final double subtotal;

  const OnlineOrderItem({
    required this.productVariantId,
    required this.quantity,
    this.productImageUrlSnapshot,
    required this.productNameSnapshot,
    this.productVariantNameSnapshot,
    required this.sellingPriceSnapshot,
    required this.subtotal,
  });

  OnlineOrderItemRequest toOnlineOrderItemRequest() {
    return OnlineOrderItemRequest(
      productVariantId: productVariantId,
      quantity: quantity,
      productImageUrlSnapshot: productImageUrlSnapshot,
      productNameSnapshot: productNameSnapshot,
      productVariantNameSnapshot: productVariantNameSnapshot,
      sellingPriceSnapshot: sellingPriceSnapshot,
      subtotal: subtotal,
    );
  }
}

class Checkout {
  final String orderId;

  final String orderNumber;

  const Checkout({required this.orderId, required this.orderNumber});
}
