import 'package:feature_inventory/feature_inventory.dart';
import 'package:feature_orders/feature_orders.dart';
import 'package:feature_wordpress/feature_wordpress.dart';

/// Aggregated summary data displayed on the mobile dashboard.
///
/// This is an app-level value object that composes data from multiple
/// feature-package repositories without leaking domain logic back into
/// those packages.
///
/// Mirrors the same domain model as `kell_web`'s `DashboardSummary`.
class DashboardSummary {
  /// Total number of inventory items.
  final int totalProducts;

  /// Items with [InventoryStatus.inStock].
  final int inStock;

  /// Items with [InventoryStatus.lowStock].
  final int lowStock;

  /// Items with [InventoryStatus.outOfStock].
  final int outOfStock;

  /// Product drafts with [PublishStatus.draft].
  final int draftProducts;

  /// Total number of orders.
  final int totalOrders;

  /// Orders with [OrderStatus.pending].
  final int pendingOrders;

  /// Orders with [OrderStatus.processing] or [OrderStatus.shipped].
  final int activeOrders;

  /// Revenue from delivered orders.
  final double deliveredRevenue;

  const DashboardSummary({
    required this.totalProducts,
    required this.inStock,
    required this.lowStock,
    required this.outOfStock,
    required this.draftProducts,
    required this.totalOrders,
    required this.pendingOrders,
    required this.activeOrders,
    required this.deliveredRevenue,
  });

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is DashboardSummary &&
          totalProducts == other.totalProducts &&
          inStock == other.inStock &&
          lowStock == other.lowStock &&
          outOfStock == other.outOfStock &&
          draftProducts == other.draftProducts &&
          totalOrders == other.totalOrders &&
          pendingOrders == other.pendingOrders &&
          activeOrders == other.activeOrders &&
          deliveredRevenue == other.deliveredRevenue;

  @override
  int get hashCode => Object.hash(
    totalProducts,
    inStock,
    lowStock,
    outOfStock,
    draftProducts,
    totalOrders,
    pendingOrders,
    activeOrders,
    deliveredRevenue,
  );

  /// An empty summary used as the initial / default state.
  static const empty = DashboardSummary(
    totalProducts: 0,
    inStock: 0,
    lowStock: 0,
    outOfStock: 0,
    draftProducts: 0,
    totalOrders: 0,
    pendingOrders: 0,
    activeOrders: 0,
    deliveredRevenue: 0,
  );

  /// Computes a [DashboardSummary] from raw repository data.
  factory DashboardSummary.fromData({
    required List<InventoryItem> inventoryItems,
    required List<ProductDraft> productDrafts,
    required List<Order> orders,
  }) {
    // Inventory counts
    final totalProducts = inventoryItems.length;
    var inStock = 0;
    var lowStock = 0;
    var outOfStock = 0;
    for (final item in inventoryItems) {
      switch (item.status) {
        case InventoryStatus.inStock:
          inStock++;
        case InventoryStatus.lowStock:
          lowStock++;
        case InventoryStatus.outOfStock:
          outOfStock++;
        case InventoryStatus.draft:
          break;
      }
    }

    // Draft product count
    final draftProducts = productDrafts.where((d) => d.status == PublishStatus.draft).length;

    // Order counts
    final totalOrders = orders.length;
    var pendingOrders = 0;
    var activeOrders = 0;
    var deliveredRevenue = 0.0;
    for (final order in orders) {
      switch (order.status) {
        case OrderStatus.pending:
          pendingOrders++;
        case OrderStatus.processing:
        case OrderStatus.shipped:
          activeOrders++;
        case OrderStatus.delivered:
          deliveredRevenue += order.total;
        case OrderStatus.cancelled:
          break;
      }
    }

    return DashboardSummary(
      totalProducts: totalProducts,
      inStock: inStock,
      lowStock: lowStock,
      outOfStock: outOfStock,
      draftProducts: draftProducts,
      totalOrders: totalOrders,
      pendingOrders: pendingOrders,
      activeOrders: activeOrders,
      deliveredRevenue: deliveredRevenue,
    );
  }
}
