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

/// A detail panel that displays full information for a selected [InventoryItem]
/// and exposes callbacks for quantity adjustment and item editing.
class InventoryDetailPanel extends StatelessWidget {
  final InventoryItem item;
  final bool isUpdating;

  /// Called when the operator taps "Adjust Quantity".
  final VoidCallback? onAdjustQuantity;

  /// Called when the operator taps "View Product" (cross-feature handoff).
  final VoidCallback? onViewProduct;

  const InventoryDetailPanel({
    super.key,
    required this.item,
    required this.isUpdating,
    this.onAdjustQuantity,
    this.onViewProduct,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return SingleChildScrollView(
      padding: const EdgeInsets.all(KcSpacing.lg),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // ── Header ──────────────────────────────────────────────────────────
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(child: Text(item.name, style: theme.textTheme.headlineSmall)),
              InventoryStatusChip(status: item.status),
            ],
          ),
          const SizedBox(height: KcSpacing.xs),
          Text(
            'SKU: ${item.sku}',
            style: theme.textTheme.bodySmall?.copyWith(color: KcColors.neutral),
          ),
          const SizedBox(height: KcSpacing.md),

          // ── Description ──────────────────────────────────────────────────────
          if (item.description.isNotEmpty) ...[
            Text(item.description, style: theme.textTheme.bodyMedium),
            const SizedBox(height: KcSpacing.md),
          ],

          const Divider(),
          const SizedBox(height: KcSpacing.md),

          // ── Stock info ───────────────────────────────────────────────────────
          KcSectionHeader(title: 'Stock'),
          const SizedBox(height: KcSpacing.sm),
          _InfoRow(label: 'Quantity on hand', value: '${item.quantityOnHand}'),
          _InfoRow(label: 'Reorder point', value: '${item.reorderPoint}'),
          _InfoRow(label: 'Unit price', value: '\$${item.unitPrice.toStringAsFixed(2)}'),
          const SizedBox(height: KcSpacing.md),

          // ── Location / supplier ──────────────────────────────────────────────
          if (item.location != null || item.supplier != null) ...[
            KcSectionHeader(title: 'Storage'),
            const SizedBox(height: KcSpacing.sm),
            if (item.location != null) _InfoRow(label: 'Location', value: item.location!),
            if (item.supplier != null) _InfoRow(label: 'Supplier', value: item.supplier!),
            const SizedBox(height: KcSpacing.md),
          ],

          // ── Category ────────────────────────────────────────────────────────
          if (item.category.isNotEmpty) ...[
            KcSectionHeader(title: 'Category'),
            const SizedBox(height: KcSpacing.sm),
            Text(item.category, style: theme.textTheme.bodyMedium),
            const SizedBox(height: KcSpacing.md),
          ],

          // ── Last updated ─────────────────────────────────────────────────────
          Text(
            'Last updated: ${_formatDate(item.lastUpdated)}',
            style: theme.textTheme.bodySmall?.copyWith(color: KcColors.neutral),
          ),

          const SizedBox(height: KcSpacing.xl),

          // ── Actions ──────────────────────────────────────────────────────────
          if (isUpdating)
            const Center(child: CircularProgressIndicator())
          else
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                FilledButton.icon(
                  onPressed: onAdjustQuantity,
                  icon: const Icon(Icons.tune),
                  label: const Text('Adjust Quantity'),
                ),
                if (onViewProduct != null) ...[
                  const SizedBox(height: KcSpacing.sm),
                  OutlinedButton.icon(
                    onPressed: onViewProduct,
                    icon: const Icon(Icons.open_in_new, size: 16),
                    label: const Text('View Product'),
                  ),
                ],
              ],
            ),
        ],
      ),
    );
  }

  static String _formatDate(DateTime dt) =>
      '${dt.year}-${dt.month.toString().padLeft(2, '0')}-${dt.day.toString().padLeft(2, '0')}';
}

// ── Private helpers ────────────────────────────────────────────────────────────

class _InfoRow extends StatelessWidget {
  final String label;
  final String value;

  const _InfoRow({required this.label, required this.value});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Padding(
      padding: const EdgeInsets.only(bottom: KcSpacing.xs),
      child: Row(
        children: [
          SizedBox(
            width: 140,
            child: Text(label, style: theme.textTheme.bodySmall?.copyWith(color: KcColors.neutral)),
          ),
          Expanded(child: Text(value, style: theme.textTheme.bodyMedium)),
        ],
      ),
    );
  }
}
