import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';

import '../../domain/order.dart';
import 'order_status_chip.dart';

/// A card displaying a summary of an [Order].
///
/// Shows the order ID, customer name, date, total, item count, and status.
/// Highlights when [isSelected] is true.
class OrderCard extends StatelessWidget {
  final Order order;
  final bool isSelected;
  final VoidCallback? onTap;

  const OrderCard({super.key, required this.order, this.isSelected = false, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        padding: const EdgeInsets.all(KcSpacing.md),
        decoration: BoxDecoration(
          color: KcColors.surface,
          border: Border.all(
            color: isSelected ? KcColors.denimBlue : KcColors.border,
            width: isSelected ? 2 : 1,
          ),
          borderRadius: BorderRadius.circular(16),
          boxShadow: const [
            BoxShadow(blurRadius: 8, offset: Offset(0, 2), color: Color(0x11000000)),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    order.id,
                    style: Theme.of(context).textTheme.titleLarge,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                OrderStatusChip(status: order.status),
              ],
            ),
            const SizedBox(height: KcSpacing.xs),
            Text(order.customerName, style: Theme.of(context).textTheme.bodyMedium),
            const SizedBox(height: KcSpacing.sm),
            Row(
              children: [
                Text(
                  '\$${order.total.toStringAsFixed(2)}',
                  style: Theme.of(
                    context,
                  ).textTheme.bodyLarge?.copyWith(fontWeight: FontWeight.w600),
                ),
                const SizedBox(width: KcSpacing.sm),
                Text(
                  '${order.itemCount} item${order.itemCount == 1 ? '' : 's'}',
                  style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: KcColors.neutral),
                ),
              ],
            ),
            const Spacer(),
            Text(
              _formatDate(order.orderDate),
              style: Theme.of(context).textTheme.bodySmall?.copyWith(color: KcColors.neutral),
            ),
          ],
        ),
      ),
    );
  }

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