import 'package:flutter/material.dart';

import '../../../../../../../common/theme/theme.dart';
import '../../../../../../../domain/analytic/analytic.dart';
import '../../../../../../components/spaces/space.dart';

class ReportDashboardOrder extends StatelessWidget {
  final DashboardAnalytic data;
  const ReportDashboardOrder({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    final successfulOrders =
        data.overview.totalOrders -
        data.overview.voidedOrders -
        data.overview.refundedOrders;

    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Ringkasan Pesanan',
            style: AppStyle.xl.copyWith(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: AppColor.textPrimary,
            ),
          ),
          const SpaceHeight(20),
          _buildSummaryItem(
            'Total Pesanan',
            '${data.overview.totalOrders}',
            Icons.shopping_cart,
            AppColor.info,
          ),
          _buildSummaryItem(
            'Pesanan Sukses',
            '$successfulOrders',
            Icons.check_circle,
            AppColor.success,
          ),
          _buildSummaryItem(
            'Pesanan Dibatalkan',
            '${data.overview.voidedOrders}',
            Icons.cancel,
            AppColor.error,
          ),
          _buildSummaryItem(
            'Pesanan Refund',
            '${data.overview.refundedOrders}',
            Icons.refresh,
            AppColor.warning,
          ),
          const SpaceHeight(20),
          // Payment Methods
          if (data.paymentMethods.isNotEmpty)
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: AppColor.primary.withOpacity(0.1),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Column(
                children: [
                  Text(
                    'Metode Pembayaran',
                    style: AppStyle.sm.copyWith(
                      fontWeight: FontWeight.w500,
                      color: AppColor.textPrimary,
                    ),
                  ),
                  const SpaceHeight(8),
                  ...data.paymentMethods.map(
                    (method) => Padding(
                      padding: const EdgeInsets.only(bottom: 4),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            method.paymentMethodType == 'cash'
                                ? Icons.payments
                                : Icons.credit_card,
                            color: AppColor.primary,
                            size: 16,
                          ),
                          const SpaceWidth(8),
                          Text(
                            method.paymentMethodName,
                            style: AppStyle.md.copyWith(
                              fontWeight: FontWeight.bold,
                              color: AppColor.primary,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildSummaryItem(
    String title,
    String value,
    IconData icon,
    Color color,
  ) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: color.withOpacity(0.1),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(icon, color: color, size: 16),
          ),
          const SizedBox(width: 12),
          Expanded(child: Text(title, style: const TextStyle(fontSize: 12))),
          Text(value, style: AppStyle.md.copyWith(fontWeight: FontWeight.w600)),
        ],
      ),
    );
  }
}
