/// 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/core/theme/colors.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 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: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w700,
                      color: AppColors.grey900,
                    ),
                  ),

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

              const SizedBox(height: 12),

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

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

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

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

  /// Build detail row
  Widget _buildDetailRow(String label, String value) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: const TextStyle(fontSize: 14, color: AppColors.grey500),
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            value,
            style: const TextStyle(fontSize: 14, color: AppColors.grey900),
          ),
        ),
      ],
    );
  }

  /// 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;
    }
  }
}
