import 'package:flutter/material.dart';

import '../../../common/extension/extension.dart';
import '../../../common/theme/theme.dart';
import '../../../domain/order/order.dart';

class OrderCard extends StatelessWidget {
  final Order order;
  final bool isActive;

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

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      decoration: BoxDecoration(
        color: isActive ? AppColor.primary.withOpacity(0.1) : AppColor.white,
        border: Border.all(
          color: isActive ? AppColor.primary : AppColor.border,
        ),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    order.orderNumber,
                    style: AppStyle.sm.copyWith(fontWeight: FontWeight.w600),
                  ),
                ),
                if (order.splitType != '')
                  Text(
                    "Split Bill: ${order.splitType}",
                    style: AppStyle.sm.copyWith(fontWeight: FontWeight.w600),
                  ),
                if (order.isRefund == true)
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 8,
                      vertical: 4,
                    ),
                    decoration: BoxDecoration(
                      color: Colors.red.withOpacity(0.15),
                      borderRadius: BorderRadius.circular(16),
                    ),
                    child: Text(
                      'Refund',
                      style: AppStyle.xs.copyWith(
                        color: AppColor.error,
                        fontWeight: FontWeight.w600,
                        fontSize: 10,
                        letterSpacing: 0.5,
                      ),
                    ),
                  ),
                if (order.isVoid == true)
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 8,
                      vertical: 4,
                    ),
                    decoration: BoxDecoration(
                      color: Colors.red.withOpacity(0.15),
                      borderRadius: BorderRadius.circular(16),
                    ),
                    child: Text(
                      'Void',
                      style: AppStyle.xs.copyWith(
                        color: AppColor.error,
                        fontWeight: FontWeight.w600,
                        fontSize: 10,
                        letterSpacing: 0.5,
                      ),
                    ),
                  ),
              ],
            ),
            const SizedBox(height: 12),
            Row(
              children: [
                const CircleAvatar(
                  radius: 22,
                  backgroundColor: AppColor.primary,
                  child: Icon(Icons.person, color: Colors.white),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        order.metadata['customer_name'] == ""
                            ? "Anonim"
                            : order.metadata['customer_name'],
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      if (order.orderType == "dineIn") ...[
                        const SizedBox(height: 4),
                        Row(
                          children: [
                            const Icon(
                              Icons.table_bar,
                              size: 16,
                              color: AppColor.textSecondary,
                            ),
                            const SizedBox(width: 4),
                            Text(
                              'Meja ${order.tableNumber}',
                              style: AppStyle.md.copyWith(
                                color: AppColor.textSecondary,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ],
                  ),
                ),
                _buildStatus(),
              ],
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  order.status == 'pending'
                      ? ((order.totalAmount) - (order.totalPaid))
                            .currencyFormatRpV2
                      : (order.totalAmount).currencyFormatRpV2,
                  style: AppStyle.xl.copyWith(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: AppColor.primary,
                  ),
                ),
                Text(
                  (order.createdAt).toFormattedDateTime(),
                  style: const TextStyle(color: AppColor.black),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatus() {
    switch (order.status) {
      case 'pending':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
          decoration: BoxDecoration(
            color: AppColor.warning.withOpacity(0.15),
            borderRadius: BorderRadius.circular(16),
          ),
          child: Text(
            (order.status).toUpperCase(),
            style: AppStyle.sm.copyWith(
              color: AppColor.warning,
              fontWeight: FontWeight.w600,
              letterSpacing: 0.5,
            ),
          ),
        );
      case 'completed':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
          decoration: BoxDecoration(
            color: AppColor.success.withOpacity(0.15),
            borderRadius: BorderRadius.circular(16),
          ),
          child: Text(
            (order.status).toUpperCase(),
            style: const TextStyle(
              color: AppColor.success,
              fontWeight: FontWeight.w600,
              fontSize: 12,
              letterSpacing: 0.5,
            ),
          ),
        );
      default:
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
          decoration: BoxDecoration(
            color: AppColor.textSecondary.withOpacity(0.15),
            borderRadius: BorderRadius.circular(16),
          ),
          child: Text(
            (order.status).toUpperCase(),
            style: const TextStyle(
              color: AppColor.textSecondary,
              fontWeight: FontWeight.w600,
              fontSize: 12,
              letterSpacing: 0.5,
            ),
          ),
        );
    }
  }
}
