import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import '../../domain/inventory_item.dart';
import '../../domain/inventory_status.dart';

class InventoryItemCard extends StatelessWidget {
  final InventoryItem item;

  /// Optional callback to navigate to the Products page for this item's SKU.
  final VoidCallback? onViewProduct;

  const InventoryItemCard({super.key, required this.item, this.onViewProduct});

  @override
  Widget build(BuildContext context) {
    final (label, bg, fg) = _statusStyle(item.status);

    return KcCard(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(item.name, style: Theme.of(context).textTheme.titleLarge),
          const SizedBox(height: KcSpacing.sm),
          Text('SKU: ${item.sku}'),
          const SizedBox(height: KcSpacing.md),
          KcStatusChip(label: label, background: bg, foreground: fg),
          const SizedBox(height: KcSpacing.md),
          Text('Quantity on hand: ${item.quantityOnHand}'),
          const SizedBox(height: KcSpacing.sm),
          Text('Unit price: \$${item.unitPrice.toStringAsFixed(2)}'),
          if (onViewProduct != null) ...[
            const SizedBox(height: KcSpacing.sm),
            GestureDetector(
              onTap: onViewProduct,
              child: Text(
                'View Product →',
                style: Theme.of(context).textTheme.bodySmall?.copyWith(
                  color: KcColors.denimBlue,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }

  (String, Color, Color) _statusStyle(InventoryStatus status) {
    switch (status) {
      case InventoryStatus.inStock:
        return ('In stock', const Color(0xFFE8F5E9), KcColors.success);
      case InventoryStatus.lowStock:
        return ('Low stock', const Color(0xFFFFF8E1), KcColors.warning);
      case InventoryStatus.outOfStock:
        return ('Out of stock', const Color(0xFFFFEBEE), KcColors.danger);
      case InventoryStatus.draft:
        return ('Draft', const Color(0xFFECEFF1), KcColors.neutral);
    }
  }
}
