import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:hyper_local/utils/widgets/custom_card.dart';
import '../../../../../utils/widgets/custom_text.dart';
import '../../../../../utils/currency_formatter.dart';
import '../../../../../l10n/app_localizations.dart';
import '../../../../../config/helper.dart';
import '../../../../../config/colors.dart';
import '../../../model/available_orders.dart';

class StoreGroup {
  final int? storeId;
  final String storeName;
  final String? storeAddress;
  final List<Widget> itemCards;

  StoreGroup({required this.storeId, required this.storeName, this.storeAddress, required this.itemCards});
}

class OrderItemsSection extends StatelessWidget {
  final Orders order;
  final bool isExpanded;
  final VoidCallback onToggle;
  final List<StoreGroup> storeGroups;
  final VoidCallback? onCollectAll;
  final Set<int> collectingStoreIds;
  final Set<String>? collectedItems;
  final Function(int)? onCollectAllForStore;

  const OrderItemsSection({
    super.key,
    required this.order,
    required this.isExpanded,
    required this.onToggle,
    required this.storeGroups,
    this.onCollectAll,
    this.collectingStoreIds = const {},
    this.collectedItems,
    this.onCollectAllForStore,
  });

  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final int itemsCount = order.items?.length ?? 0;
    final String itemsText = itemsCount == 1 ? '1 Item' : '$itemsCount Items';

    final double subtotalVal = double.tryParse(order.subtotal ?? '0') ?? 0;
    final double deliveryVal = double.tryParse(order.deliveryCharge ?? '0') ?? 0;
    final double finalTotalVal = double.tryParse(order.finalTotal ?? '0') ?? 0;

    // Calculate other fees (like handling charges or drop off fee) if there is a discrepancy
    final double otherFeesVal = finalTotalVal - (subtotalVal + deliveryVal);

    return CustomCard(
      padding: EdgeInsets.zero,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 14.h),
            child: Row(
              children: [
                Icon(
                  TablerIcons.box,
                  color: isDark ? Colors.grey[400] : Colors.grey[700],
                  size: sz(22, seprateTabletSize: 18).sp,
                ),
                SizedBox(width: 8.w),
                CustomText(
                  text: AppLocalizations.of(context)!.orderItems,
                  fontSize: sz(16, seprateTabletSize: 13),
                  fontWeight: FontWeight.w500,
                  color: const Color(0xFF6B6B6B),
                ),
              ],
            ),
          ),

          // Divider
          Divider(height: 1, thickness: 1, color: isDark ? Colors.grey[800] : Colors.grey[200]),

          // Items List
          Padding(
            padding: EdgeInsets.fromLTRB(16.w, 12.h, 16.w, 8.h),
            child: Column(
              children: [
                if (storeGroups.isNotEmpty) ...[
                  for (int i = 0; i < storeGroups.length; i++) ...[
                    if (i > 0)
                      Padding(
                        padding: EdgeInsets.symmetric(vertical: 12.h),
                        child: Divider(height: 1, thickness: 1, color: isDark ? Colors.grey[800] : Colors.grey[200]),
                      ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // Store Header
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 8.h),
                          decoration: BoxDecoration(
                            color:
                                isDark
                                    ? Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.4)
                                    : AppColors.primaryColor.withValues(alpha: 0.05),
                            borderRadius: BorderRadius.circular(8.r),
                          ),
                          child: Row(
                            children: [
                              Icon(
                                TablerIcons.building_store,
                                color: AppColors.primaryColor,
                                size: sz(20, seprateTabletSize: 16).sp,
                              ),
                              SizedBox(width: 10.w),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [
                                        Expanded(
                                          child: CustomText(
                                            text: storeGroups[i].storeName,
                                            fontSize: sz(14, seprateTabletSize: 12),
                                            fontWeight: FontWeight.bold,
                                            color: Theme.of(context).colorScheme.onSurface,
                                          ),
                                        ),
                                        Container(
                                          padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 4.h),
                                          decoration: BoxDecoration(
                                            color: isDark ? Colors.grey[800] : Colors.grey[200],
                                            borderRadius: BorderRadius.circular(12.r),
                                          ),
                                          child: CustomText(
                                            text:
                                                storeGroups[i].itemCards.length == 1
                                                    ? '1 ${AppLocalizations.of(context)!.item}'
                                                    : '${storeGroups[i].itemCards.length} ${AppLocalizations.of(context)!.items}',
                                            fontSize: sz(11, seprateTabletSize: 9),
                                            fontWeight: FontWeight.w600,
                                            color: isDark ? Colors.grey[300] : Colors.grey[700],
                                          ),
                                        ),
                                      ],
                                    ),
                                    if (storeGroups[i].storeAddress != null &&
                                        storeGroups[i].storeAddress!.isNotEmpty) ...[
                                      SizedBox(height: 2.h),
                                      CustomText(
                                        text: storeGroups[i].storeAddress!,
                                        fontSize: sz(11, seprateTabletSize: 9),
                                        color: isDark ? Colors.grey[400] : Colors.grey[600],
                                      ),
                                    ],
                                    const SizedBox(height: 4),
                                    Align(
                                      alignment: AlignmentGeometry.centerRight,
                                      child: Builder(
                                        builder: (context) {
                                          final orderStatus = order.status?.toLowerCase();
                                          if (orderStatus == 'cancelled' || orderStatus == 'canceled') {
                                            return const SizedBox.shrink();
                                          }

                                          final storeId = storeGroups[i].storeId;
                                          if (storeId == null) return const SizedBox.shrink();

                                          final hasUncollectedItems =
                                              order.items?.any((item) {
                                                if (item.storeId != storeId) return false;
                                                final s = item.status?.toLowerCase();
                                                return s != 'collected' &&
                                                    s != 'delivered' &&
                                                    s != 'returning_to_store' &&
                                                    !(collectedItems?.contains(item.id.toString()) ?? false);
                                              }) ??
                                              false;

                                          if (!hasUncollectedItems || onCollectAllForStore == null) {
                                            return const SizedBox.shrink();
                                          }

                                          final isThisStoreCollecting = collectingStoreIds.contains(storeId);

                                          return Container(
                                            decoration: BoxDecoration(
                                              gradient: LinearGradient(
                                                colors:
                                                    isThisStoreCollecting
                                                        ? [Colors.grey[400]!, Colors.grey[500]!]
                                                        : [
                                                          AppColors.primaryColor,
                                                          AppColors.primaryColor.withValues(alpha: 0.85),
                                                        ],
                                              ),
                                              borderRadius: BorderRadius.circular(20.r),
                                              boxShadow:
                                                  isThisStoreCollecting
                                                      ? null
                                                      : [
                                                        BoxShadow(
                                                          color: AppColors.primaryColor.withValues(alpha: 0.2),
                                                          blurRadius: 4,
                                                          offset: const Offset(0, 2),
                                                        ),
                                                      ],
                                            ),
                                            child: InkWell(
                                              borderRadius: BorderRadius.circular(20.r),
                                              onTap:
                                                  isThisStoreCollecting ? null : () => onCollectAllForStore!(storeId),
                                              child: Padding(
                                                padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
                                                child: Row(
                                                  mainAxisSize: MainAxisSize.min,
                                                  children: [
                                                    Icon(
                                                      TablerIcons.circle_check,
                                                      color: Colors.white,
                                                      size: sz(13, seprateTabletSize: 10).sp,
                                                    ),
                                                    SizedBox(width: 4.w),
                                                    CustomText(
                                                      text: AppLocalizations.of(context)!.collectAllItems,
                                                      fontSize: sz(10, seprateTabletSize: 8),
                                                      fontWeight: FontWeight.bold,
                                                      color: Colors.white,
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                          );
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 8.h),
                        // Store Items List
                        Padding(padding: EdgeInsets.only(left: 4.w), child: Column(children: storeGroups[i].itemCards)),
                      ],
                    ),
                  ],
                ] else
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 16.h),
                    child: CustomText(
                      text: AppLocalizations.of(context)!.noData,
                      fontSize: sz(16, seprateTabletSize: 12),
                      color: Colors.grey,
                    ),
                  ),
              ],
            ),
          ),

          // Optional Price Breakdown (Subtotal, Delivery, Handling, etc.)
          if (itemsCount > 0) ...[
            Divider(height: 1, thickness: 1, color: isDark ? Colors.grey[800] : Colors.grey[200]),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h),
              child: Column(
                children: [
                  _buildPriceRow(
                    context,
                    '${AppLocalizations.of(context)!.subtotal} :',
                    CurrencyFormatter.formatAmount(context, order.subtotal ?? '0'),
                  ),
                  if (deliveryVal > 0)
                    _buildPriceRow(
                      context,
                      '${AppLocalizations.of(context)!.deliveryCharges} :',
                      CurrencyFormatter.formatAmount(context, order.deliveryCharge ?? '0'),
                    ),
                  if (otherFeesVal > 0.01)
                    _buildPriceRow(
                      context,
                      '${AppLocalizations.of(context)!.otherCharges} :',
                      CurrencyFormatter.formatAmount(context, otherFeesVal.toStringAsFixed(2)),
                    ),
                ],
              ),
            ),
          ],

          // Divider above Total
          Divider(height: 1, thickness: 1, color: isDark ? Colors.grey[800] : Colors.grey[200]),

          // Bottom Total Container
          Container(
            width: double.infinity,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                CustomText(
                  text: '${AppLocalizations.of(context)!.total} ($itemsText)',
                  fontSize: sz(15, seprateTabletSize: 12),
                  fontWeight: FontWeight.w500,
                ),
                CustomText(
                  text: CurrencyFormatter.formatAmount(context, order.finalTotal ?? '0'),
                  fontSize: sz(18, seprateTabletSize: 14),
                  fontWeight: FontWeight.bold,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPriceRow(BuildContext context, String label, String amount) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 4.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [CustomText(text: label), CustomText(text: amount, fontWeight: FontWeight.w500)],
      ),
    );
  }
}
