import '../domain/order.dart';
import '../domain/order_action_result.dart';
import '../domain/order_item.dart';
import '../domain/order_status.dart';
import '../domain/orders_repository.dart';

/// Stubbed implementation of [OrdersRepository] with sample
/// Kell Creations orders. No real WooCommerce or shipping API calls are made.
class FakeOrdersRepository implements OrdersRepository {
  final List<Order> _orders = [
    Order(
      id: 'KC-1001',
      customerName: 'Sarah Mitchell',
      customerEmail: 'sarah.mitchell@example.com',
      customerPhone: '828-555-0101',
      orderDate: DateTime(2026, 4, 1),
      status: OrderStatus.delivered,
      shippingAddress: '123 Maple St, Asheville, NC 28801',
      items: const [
        OrderItem(
          productName: 'Floral Bowl Cozy',
          sku: 'BC-FLR-001',
          quantity: 2,
          unitPrice: 12.99,
        ),
        OrderItem(
          productName: 'Citrus Coaster Set',
          sku: 'CS-CIT-002',
          quantity: 1,
          unitPrice: 16.50,
        ),
      ],
    ),
    Order(
      id: 'KC-1002',
      customerName: 'James Thornton',
      customerEmail: 'james.thornton@example.com',
      orderDate: DateTime(2026, 4, 2),
      status: OrderStatus.shipped,
      shippingAddress: '456 Oak Ave, Knoxville, TN 37902',
      items: const [
        OrderItem(
          productName: 'Ocean Nightlight',
          sku: 'NL-OCN-003',
          quantity: 1,
          unitPrice: 19.99,
        ),
        OrderItem(
          productName: 'Sublimated Slate Coaster',
          sku: 'SC-SUB-006',
          quantity: 3,
          unitPrice: 14.99,
        ),
      ],
    ),
    Order(
      id: 'KC-1003',
      customerName: 'Emily Chen',
      customerEmail: 'emily.chen@example.com',
      customerPhone: '704-555-0303',
      orderDate: DateTime(2026, 4, 3),
      status: OrderStatus.processing,
      shippingAddress: '789 Pine Rd, Charlotte, NC 28202',
      items: const [
        OrderItem(
          productName: 'Fabric Jar Gripper',
          sku: 'JG-BLU-004',
          quantity: 4,
          unitPrice: 8.50,
        ),
      ],
    ),
    Order(
      id: 'KC-1004',
      customerName: 'David Park',
      customerEmail: 'david.park@example.com',
      orderDate: DateTime(2026, 4, 3),
      status: OrderStatus.pending,
      shippingAddress: '321 Birch Ln, Greenville, SC 29601',
      items: const [
        OrderItem(
          productName: 'Skillet Handle Sleeve',
          sku: 'SH-SUN-005',
          quantity: 2,
          unitPrice: 10.99,
        ),
        OrderItem(
          productName: 'Floral Bowl Cozy',
          sku: 'BC-FLR-001',
          quantity: 1,
          unitPrice: 12.99,
        ),
      ],
    ),
    Order(
      id: 'KC-1005',
      customerName: 'Rachel Adams',
      customerEmail: 'rachel.adams@example.com',
      orderDate: DateTime(2026, 3, 28),
      status: OrderStatus.cancelled,
      shippingAddress: '654 Elm St, Richmond, VA 23220',
      items: const [
        OrderItem(
          productName: 'Citrus Coaster Set',
          sku: 'CS-CIT-002',
          quantity: 2,
          unitPrice: 16.50,
        ),
      ],
    ),
    Order(
      id: 'KC-1006',
      customerName: 'Maria Gonzalez',
      customerEmail: 'maria.gonzalez@example.com',
      customerPhone: '404-555-0606',
      orderDate: DateTime(2026, 4, 4),
      status: OrderStatus.pending,
      shippingAddress: '987 Cedar Dr, Atlanta, GA 30301',
      items: const [
        OrderItem(
          productName: 'Ocean Nightlight',
          sku: 'NL-OCN-003',
          quantity: 1,
          unitPrice: 19.99,
        ),
        OrderItem(
          productName: 'Fabric Jar Gripper',
          sku: 'JG-BLU-004',
          quantity: 2,
          unitPrice: 8.50,
        ),
        OrderItem(
          productName: 'Sublimated Slate Coaster',
          sku: 'SC-SUB-006',
          quantity: 1,
          unitPrice: 14.99,
        ),
      ],
    ),
  ];

  @override
  Future<List<Order>> getOrders() async {
    // Simulate network latency.
    await Future<void>.delayed(const Duration(milliseconds: 300));
    return List.unmodifiable(_orders);
  }

  @override
  Future<OrderActionResult> updateOrderStatus(String id, OrderStatus newStatus) async {
    await Future<void>.delayed(const Duration(milliseconds: 200));
    final index = _orders.indexWhere((o) => o.id == id);
    if (index == -1) {
      return const OrderActionResult.failure('Order not found.');
    }
    final updated = _orders[index].copyWith(status: newStatus);
    _orders[index] = updated;
    return OrderActionResult.success(updated);
  }

  @override
  Future<OrderActionResult> addOrderNote(String id, String note) async {
    await Future<void>.delayed(const Duration(milliseconds: 200));
    if (note.trim().isEmpty) {
      return const OrderActionResult.failure('Note cannot be empty.');
    }
    final index = _orders.indexWhere((o) => o.id == id);
    if (index == -1) {
      return const OrderActionResult.failure('Order not found.');
    }
    final existing = _orders[index];
    final updated = existing.copyWith(orderNotes: [...existing.orderNotes, note.trim()]);
    _orders[index] = updated;
    return OrderActionResult.success(updated);
  }

  @override
  Future<OrderActionResult> createOrder(Order order) async {
    await Future<void>.delayed(const Duration(milliseconds: 200));
    if (_orders.any((o) => o.id == order.id)) {
      return OrderActionResult.failure('Order with ID ${order.id} already exists.');
    }
    _orders.add(order);
    return OrderActionResult.success(order);
  }
}
