import 'package:feature_inventory/feature_inventory.dart';
import 'package:feature_orders/feature_orders.dart';
import 'package:feature_wordpress/feature_wordpress.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kell_web/dashboard/domain/dashboard_summary.dart';

// ── Shared test helpers ────────────────────────────────────────────────────

final _baseDate = DateTime(2026, 7, 1);

InventoryItem _inv({
  required String id,
  required String sku,
  required String name,
  required int quantityOnHand,
  required double unitPrice,
  required InventoryStatus status,
  int reorderPoint = 5,
}) {
  return InventoryItem(
    id: id,
    sku: sku,
    name: name,
    description: '',
    imageUrl: '',
    category: 'Test',
    quantityOnHand: quantityOnHand,
    unitPrice: unitPrice,
    reorderPoint: reorderPoint,
    status: status,
    lastUpdated: _baseDate,
  );
}

void main() {
  group('DashboardSummary.empty', () {
    test('has all zeroes', () {
      const s = DashboardSummary.empty;
      expect(s.totalProducts, 0);
      expect(s.inStock, 0);
      expect(s.lowStock, 0);
      expect(s.outOfStock, 0);
      expect(s.draftProducts, 0);
      expect(s.totalOrders, 0);
      expect(s.pendingOrders, 0);
      expect(s.activeOrders, 0);
      expect(s.deliveredRevenue, 0.0);
    });
  });

  group('DashboardSummary.fromData', () {
    test('counts inventory statuses correctly', () {
      final items = [
        _inv(
          id: '1',
          sku: 'A',
          name: 'A',
          quantityOnHand: 10,
          unitPrice: 5.0,
          status: InventoryStatus.inStock,
        ),
        _inv(
          id: '2',
          sku: 'B',
          name: 'B',
          quantityOnHand: 2,
          unitPrice: 5.0,
          status: InventoryStatus.lowStock,
        ),
        _inv(
          id: '3',
          sku: 'C',
          name: 'C',
          quantityOnHand: 0,
          unitPrice: 5.0,
          status: InventoryStatus.outOfStock,
        ),
        _inv(
          id: '4',
          sku: 'D',
          name: 'D',
          quantityOnHand: 0,
          unitPrice: 5.0,
          status: InventoryStatus.draft,
        ),
      ];

      final summary = DashboardSummary.fromData(
        inventoryItems: items,
        productDrafts: [],
        orders: [],
      );

      expect(summary.totalProducts, 4);
      expect(summary.inStock, 1);
      expect(summary.lowStock, 1);
      expect(summary.outOfStock, 1);
    });

    test('counts draft products from publishing repository', () {
      final drafts = [
        ProductDraft(
          id: '1',
          name: 'A',
          description: '',
          price: 10,
          sku: 'A',
          category: 'Cat',
          imageUrl: '',
          status: PublishStatus.draft,
          lastModified: DateTime(2026),
        ),
        ProductDraft(
          id: '2',
          name: 'B',
          description: '',
          price: 10,
          sku: 'B',
          category: 'Cat',
          imageUrl: '',
          status: PublishStatus.published,
          lastModified: DateTime(2026),
        ),
        ProductDraft(
          id: '3',
          name: 'C',
          description: '',
          price: 10,
          sku: 'C',
          category: 'Cat',
          imageUrl: '',
          status: PublishStatus.draft,
          lastModified: DateTime(2026),
        ),
      ];

      final summary = DashboardSummary.fromData(
        inventoryItems: [],
        productDrafts: drafts,
        orders: [],
      );

      expect(summary.draftProducts, 2);
    });

    test('counts order statuses and revenue correctly', () {
      final orders = [
        Order(
          id: '1',
          customerName: 'A',
          customerEmail: 'a@a.com',
          orderDate: DateTime(2026),
          status: OrderStatus.pending,
          items: const [OrderItem(productName: 'X', sku: 'X', quantity: 1, unitPrice: 10.0)],
          shippingAddress: '',
        ),
        Order(
          id: '2',
          customerName: 'B',
          customerEmail: 'b@b.com',
          orderDate: DateTime(2026),
          status: OrderStatus.processing,
          items: const [OrderItem(productName: 'X', sku: 'X', quantity: 2, unitPrice: 5.0)],
          shippingAddress: '',
        ),
        Order(
          id: '3',
          customerName: 'C',
          customerEmail: 'c@c.com',
          orderDate: DateTime(2026),
          status: OrderStatus.shipped,
          items: const [OrderItem(productName: 'X', sku: 'X', quantity: 1, unitPrice: 20.0)],
          shippingAddress: '',
        ),
        Order(
          id: '4',
          customerName: 'D',
          customerEmail: 'd@d.com',
          orderDate: DateTime(2026),
          status: OrderStatus.delivered,
          items: const [OrderItem(productName: 'X', sku: 'X', quantity: 3, unitPrice: 10.0)],
          shippingAddress: '',
        ),
        Order(
          id: '5',
          customerName: 'E',
          customerEmail: 'e@e.com',
          orderDate: DateTime(2026),
          status: OrderStatus.cancelled,
          items: const [OrderItem(productName: 'X', sku: 'X', quantity: 1, unitPrice: 100.0)],
          shippingAddress: '',
        ),
      ];

      final summary = DashboardSummary.fromData(
        inventoryItems: [],
        productDrafts: [],
        orders: orders,
      );

      expect(summary.totalOrders, 5);
      expect(summary.pendingOrders, 1);
      expect(summary.activeOrders, 2); // processing + shipped
      expect(summary.deliveredRevenue, 30.0); // 3 * 10
    });

    test('computes full summary from fake repository data', () {
      final summary = DashboardSummary.fromData(
        inventoryItems: [
          _inv(
            id: '1',
            sku: 'BC-FLR-001',
            name: 'Floral Bowl Cozy',
            quantityOnHand: 18,
            unitPrice: 12.99,
            status: InventoryStatus.inStock,
          ),
          _inv(
            id: '2',
            sku: 'CS-CIT-002',
            name: 'Citrus Coaster Set',
            quantityOnHand: 7,
            unitPrice: 16.50,
            status: InventoryStatus.lowStock,
            reorderPoint: 10,
          ),
          _inv(
            id: '3',
            sku: 'NL-OCN-003',
            name: 'Ocean Nightlight',
            quantityOnHand: 0,
            unitPrice: 19.99,
            status: InventoryStatus.outOfStock,
          ),
          _inv(
            id: '4',
            sku: 'JG-BLU-004',
            name: 'Fabric Jar Gripper',
            quantityOnHand: 23,
            unitPrice: 8.50,
            status: InventoryStatus.inStock,
          ),
          _inv(
            id: '5',
            sku: 'SH-SUN-005',
            name: 'Skillet Handle Sleeve',
            quantityOnHand: 5,
            unitPrice: 10.99,
            status: InventoryStatus.lowStock,
          ),
          _inv(
            id: '6',
            sku: 'SC-SUB-006',
            name: 'Sublimated Slate Coaster',
            quantityOnHand: 0,
            unitPrice: 14.99,
            status: InventoryStatus.draft,
          ),
        ],
        productDrafts: [
          ProductDraft(
            id: '4',
            name: 'Fabric Jar Gripper',
            description: '',
            price: 8.50,
            sku: 'JG-BLU-004',
            category: 'Kitchen Accessories',
            imageUrl: '',
            status: PublishStatus.draft,
            lastModified: DateTime(2026, 4, 2),
          ),
          ProductDraft(
            id: '5',
            name: 'Skillet Handle Sleeve',
            description: '',
            price: 10.99,
            sku: 'SH-SUN-005',
            category: 'Kitchen Accessories',
            imageUrl: '',
            status: PublishStatus.draft,
            lastModified: DateTime(2026, 4, 3),
          ),
        ],
        orders: [
          Order(
            id: 'KC-1001',
            customerName: 'Sarah Mitchell',
            customerEmail: 'sarah@example.com',
            orderDate: DateTime(2026, 4, 1),
            status: OrderStatus.delivered,
            shippingAddress: '123 Maple St',
            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@example.com',
            orderDate: DateTime(2026, 4, 2),
            status: OrderStatus.shipped,
            shippingAddress: '456 Oak Ave',
            items: const [
              OrderItem(
                productName: 'Ocean Nightlight',
                sku: 'NL-OCN-003',
                quantity: 1,
                unitPrice: 19.99,
              ),
            ],
          ),
          Order(
            id: 'KC-1003',
            customerName: 'Emily Chen',
            customerEmail: 'emily@example.com',
            orderDate: DateTime(2026, 4, 3),
            status: OrderStatus.pending,
            shippingAddress: '789 Pine Rd',
            items: const [
              OrderItem(
                productName: 'Fabric Jar Gripper',
                sku: 'JG-BLU-004',
                quantity: 4,
                unitPrice: 8.50,
              ),
            ],
          ),
        ],
      );

      expect(summary.totalProducts, 6);
      expect(summary.inStock, 2);
      expect(summary.lowStock, 2);
      expect(summary.outOfStock, 1);
      expect(summary.draftProducts, 2);
      expect(summary.totalOrders, 3);
      expect(summary.pendingOrders, 1);
      expect(summary.activeOrders, 1); // shipped only
      // delivered revenue: 2*12.99 + 1*16.50 = 42.48
      expect(summary.deliveredRevenue, closeTo(42.48, 0.01));
    });
  });
}
