/// Widget: Order Card
///
/// Displays order information in a card format.
library;

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:worker/core/enums/status_color.dart';
import 'package:worker/features/orders/domain/entities/order.dart';

/// Order Card Widget
///
/// Displays order details in a card with status indicator.
class OrderCard extends StatelessWidget {
  /// Order to display
  final Order order;

  /// Tap callback
  final VoidCallback? onTap;

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

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    final currencyFormatter = NumberFormat.currency(
      locale: 'vi_VN',
      symbol: 'đ',
      decimalDigits: 0,
    );

    return Card(
      margin: const EdgeInsets.only(bottom: 12),
      elevation: 1,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      clipBehavior: Clip.antiAlias,
      child: InkWell(
        onTap: onTap,
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Order number and amount row
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Order number
                  Text(
                    '#${order.name}',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w700,
                      color: colorScheme.onSurface,
                    ),
                  ),

                  // Amount
                  Text(
                    currencyFormatter.format(order.grandTotal),
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w700,
                      color: colorScheme.primary,
                    ),
                  ),
                ],
              ),

              const SizedBox(height: 12),

              // Order details
              _buildDetailRow(
                context,
                'Ngày đặt:',
                _formatDate(order.transactionDate),
              ),
              const SizedBox(height: 6),

              _buildDetailRow(
                context,
                'Ngày giao:',
                _formatDate(order.deliveryDate),
              ),
              const SizedBox(height: 6),

              _buildDetailRow(context, 'Địa chỉ:', order.address),
              const SizedBox(height: 12),

              // Status badge
              _buildStatusBadge(),
            ],
          ),
        ),
      ),
    );
  }

  /// Build detail row
  Widget _buildDetailRow(BuildContext context, String label, String value) {
    final colorScheme = Theme.of(context).colorScheme;

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14,
            color: colorScheme.onSurfaceVariant,
          ),
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              fontSize: 14,
              color: colorScheme.onSurface,
            ),
          ),
        ),
      ],
    );
  }

  /// Build status badge
  Widget _buildStatusBadge() {
    final statusColor = _getStatusColor();

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: statusColor.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(16),
        border: Border.all(
          color: statusColor.withValues(alpha: 0.3),
          width: 1,
        ),
      ),
      child: Text(
        order.status,
        style: TextStyle(
          fontSize: 13,
          fontWeight: FontWeight.w600,
          color: statusColor,
        ),
      ),
    );
  }

  /// Get status color from API status_color field
  Color _getStatusColor() {
    // Parse statusColor from API (Warning, Success, Danger, Info, Secondary)
    final statusColorEnum = StatusColor.values.firstWhere(
      (e) => e.name.toLowerCase() == order.statusColor.toLowerCase(),
      orElse: () => StatusColor.secondary,
    );
    return statusColorEnum.color;
  }

  /// Format date to dd/MM/yyyy
  String _formatDate(String? dateString) {
    if (dateString == null || dateString.isEmpty) {
      return 'Chưa xác định';
    }

    try {
      final date = DateTime.parse(dateString);
      return DateFormat('dd/MM/yyyy').format(date);
    } catch (e) {
      return dateString;
    }
  }
}
