import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/data/models/response/category_analytic_response_model.dart';
import 'package:flutter/material.dart';

class CategoryReportWidget extends StatelessWidget {
  final String title;
  final String searchDateFormatted;
  final CategoryAnalyticData categoryAnalyticData;
  const CategoryReportWidget({
    super.key,
    required this.title,
    required this.searchDateFormatted,
    required this.categoryAnalyticData,
  });

  String formatCurrency(int amount) {
    return 'Rp ${amount.toString().replaceAllMapped(
          RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'),
          (Match m) => '${m[1]}.',
        )}';
  }

  int getTotalRevenue() {
    return categoryAnalyticData.data
        .fold(0, (sum, item) => sum + item.totalRevenue);
  }

  int getTotalQuantity() {
    return categoryAnalyticData.data
        .fold(0, (sum, item) => sum + item.totalQuantity);
  }

  int getTotalOrders() {
    return categoryAnalyticData.data
        .fold(0, (sum, item) => sum + item.orderCount);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: AppColors.background,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header Section
          Container(
            width: double.infinity,
            color: AppColors.primary,
            padding: const EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                    color: AppColors.whiteText,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  'Periode: $searchDateFormatted',
                  style: TextStyle(
                    fontSize: 14,
                    color: AppColors.whiteText.withOpacity(0.8),
                  ),
                ),
              ],
            ),
          ),

          // Summary Cards Section
          Container(
            color: AppColors.white,
            padding: const EdgeInsets.all(20),
            child: Row(
              children: [
                Expanded(
                  child: _buildSummaryCard(
                    'Total Pendapatan',
                    formatCurrency(getTotalRevenue()),
                    AppColors.green,
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: _buildSummaryCard(
                    'Total Item Terjual',
                    '${getTotalQuantity()} pcs',
                    AppColors.subtitle,
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: _buildSummaryCard(
                    'Total Pesanan',
                    '${getTotalOrders()}',
                    AppColors.primary,
                  ),
                ),
              ],
            ),
          ),

          // Categories List Header
          Container(
            color: AppColors.stroke,
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
            child: Row(
              children: [
                Expanded(
                  flex: 3,
                  child: Text(
                    'Kategori',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: AppColors.black,
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Text(
                    'Pendapatan',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: AppColors.black,
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Text(
                    'Qty',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: AppColors.black,
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Text(
                    'Order',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: AppColors.black,
                    ),
                  ),
                ),
              ],
            ),
          ),

          // Categories List
          Expanded(
            child: Container(
              color: AppColors.white,
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: categoryAnalyticData.data.length,
                itemBuilder: (context, index) {
                  return _buildCategoryItem(
                      categoryAnalyticData.data[index], index);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSummaryCard(String title, String value, Color color) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.light,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColors.stroke, width: 1),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 12,
              color: AppColors.subtitle,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            value,
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
              color: color,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCategoryItem(CategoryAnalyticItem category, int index) {
    final isEven = index % 2 == 0;

    return Container(
      color: isEven ? AppColors.white : AppColors.light.withOpacity(0.3),
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
      child: Row(
        children: [
          Expanded(
            flex: 3,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  category.categoryName,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: AppColors.black,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  '${category.productCount} produk',
                  style: TextStyle(
                    fontSize: 12,
                    color: AppColors.subtitle,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 2,
            child: Column(
              children: [
                Text(
                  formatCurrency(category.totalRevenue),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w600,
                    color: AppColors.green,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 1,
            child: Text(
              '${category.totalQuantity}',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
                color: AppColors.black,
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: Text(
              '${category.orderCount}',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
                color: AppColors.black,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
