/// Cart Page
///
/// Shopping cart screen with selection and checkout.
/// Features expanded item list with total price at bottom.
library;
import 'package:worker/core/utils/extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:worker/core/widgets/loading_indicator.dart';
import 'package:worker/core/constants/ui_constants.dart';
import 'package:worker/core/router/app_router.dart';
import 'package:worker/core/theme/colors.dart';
import 'package:worker/core/theme/typography.dart';
import 'package:worker/features/cart/presentation/providers/cart_provider.dart';
import 'package:worker/features/cart/presentation/providers/cart_state.dart';
import 'package:worker/features/cart/presentation/widgets/cart_item_widget.dart';
import 'package:worker/core/services/analytics_service.dart';
import 'package:firebase_analytics/firebase_analytics.dart';

/// Cart Page
///
/// Features:
/// - AppBar with back, title (with count), and delete button
/// - Select all section with count display
/// - Expanded cart items list with checkboxes
/// - Total price and checkout button at bottom
class CartPage extends ConsumerStatefulWidget {
  const CartPage({super.key});

  @override
  ConsumerState<CartPage> createState() => _CartPageState();
}

class _CartPageState extends ConsumerState<CartPage> {
  bool _isSyncing = false;

  // Cart is initialized once in home_page.dart at app startup
  // Provider has keepAlive: true, so no need to reload here

  // Note: Sync is handled in PopScope.onPopInvokedWithResult for back navigation
  // and in checkout button handler for checkout flow.
  // No dispose() method needed - using ref.read() in dispose() is unsafe.

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    final cartState = ref.watch(cartProvider);



    final itemCount = cartState.itemCount;
    final hasSelection = cartState.selectedCount > 0;

    // Log view cart analytics event when cart has items
    if (cartState.isNotEmpty) {
      AnalyticsService.logViewCart(
        cartValue: cartState.selectedTotal,
        items: cartState.items.map((item) => AnalyticsEventItem(
          itemId: item.product.productId,
          itemName: item.product.name,
          price: item.product.basePrice,
          quantity: item.quantity.toInt(),
        )).toList(),
      );
    }

    return PopScope(
      // Intercept back navigation to sync pending updates
      onPopInvokedWithResult: (didPop, result) async {
        if (didPop) {
          // Sync any pending quantity updates before leaving the page
          await ref.read(cartProvider.notifier).forceSyncPendingUpdates();
        }
      },
      child: Scaffold(
        backgroundColor: colorScheme.surfaceContainerLowest,
        appBar: AppBar(
          leading: IconButton(
            icon: FaIcon(FontAwesomeIcons.arrowLeft, color: colorScheme.onSurface, size: 20),
            onPressed: () => context.pop(),
          ),
        title: Text(
          'Giỏ hàng ($itemCount)',
          style: TextStyle(color: colorScheme.onSurface),
        ),
        elevation: AppBarSpecs.elevation,
        backgroundColor: colorScheme.surface,
        foregroundColor: colorScheme.onSurface,
        centerTitle: false,
        actions: [
          if (cartState.isNotEmpty)
            IconButton(
              icon: Icon(
                FontAwesomeIcons.trashCan,
                color: hasSelection ? AppColors.danger : colorScheme.onSurfaceVariant,
              ),
              onPressed: hasSelection
                  ? () {
                      _showDeleteConfirmation(context, ref, cartState);
                    }
                  : null,
              tooltip: 'Xóa sản phẩm đã chọn',
            ),
          const SizedBox(width: AppSpacing.sm),
        ],
      ),
      body: cartState.isLoading && cartState.isEmpty
          ? const CustomLoadingIndicator()
          : cartState.errorMessage != null && cartState.isEmpty
              ? _buildErrorState(context, cartState.errorMessage!)
              : cartState.isEmpty
                  ? _buildEmptyCart(context)
                  : Column(
                      children: [
                        // Error banner if there's an error
                        if (cartState.errorMessage != null)
                          _buildErrorBanner(cartState.errorMessage!),

                        // Select All Section
                        const SizedBox(height: 8),
                        _buildSelectAllSection(cartState, ref),
                        const SizedBox(height: 8),

                        // Expanded Cart Items List
                        Expanded(
                          child: Stack(
                            children: [
                              ListView.builder(
                                itemCount: cartState.items.length,
                                itemBuilder: (context, index) {
                                  return CartItemWidget(item: cartState.items[index]);
                                },
                              ),
                              // Loading overlay
                              if (cartState.isLoading)
                                Container(
                                  color: colorScheme.onSurface.withValues(alpha: 0.1),
                                  child: const CustomLoadingIndicator(),
                                ),
                            ],
                          ),
                        ),

                        // Total and Checkout at Bottom
                        _buildBottomSection(
                          context,
                          cartState,
                          ref,
                          NumberFormat.currency(
                            locale: 'vi_VN',
                            symbol: 'đ',
                            decimalDigits: 0,
                          ),
                          hasSelection,
                        ),
                      ],
                    ),
      ),
    );
  }

  /// Build select all section
  Widget _buildSelectAllSection(CartState cartState, WidgetRef ref) {
    final colorScheme = Theme.of(context).colorScheme;

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
      decoration: BoxDecoration(
        color: colorScheme.surface,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: colorScheme.onSurface.withValues(alpha: 0.05),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // Checkbox with label
          GestureDetector(
            onTap: () {
              ref.read(cartProvider.notifier).toggleSelectAll();
            },
            child: Row(
              children: [
                _CustomCheckbox(
                  value: cartState.isAllSelected,
                  onChanged: (value) {
                    ref.read(cartProvider.notifier).toggleSelectAll();
                  },
                ),
                const SizedBox(width: 12),
                Text(
                  'Chọn tất cả',
                  style: AppTypography.titleMedium.copyWith(
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),

          // Selected count
          Text(
            'Đã chọn: ${cartState.selectedCount}/${cartState.itemCount}',
            style: AppTypography.bodyMedium.copyWith(
              color: colorScheme.primary,
              fontWeight: FontWeight.w600,
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }

  /// Build bottom section with total price and checkout button
  Widget _buildBottomSection(
    BuildContext context,
    CartState cartState,
    WidgetRef ref,
    NumberFormat currencyFormatter,
    bool hasSelection,
  ) {
    final colorScheme = Theme.of(context).colorScheme;

    return Container(
      decoration: BoxDecoration(
        color: colorScheme.surface,
        border: Border(
          top: BorderSide(color: colorScheme.outlineVariant, width: 2),
        ),
        boxShadow: [
          BoxShadow(
            color: colorScheme.onSurface.withValues(alpha: 0.08),
            blurRadius: 10,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // Total Price Row
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'Tổng tạm tính (${cartState.selectedCount} sản phẩm)',
                    style: AppTypography.bodyMedium.copyWith(
                      color: colorScheme.onSurfaceVariant,
                      fontSize: 14,
                    ),
                  ),
                  Text(
                    currencyFormatter.format(cartState.selectedTotal),
                    style: AppTypography.headlineSmall.copyWith(
                      color: colorScheme.primary,
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                ],
              ),

              const SizedBox(height: 16),

              // Checkout Button
              SizedBox(
                width: double.infinity,
                height: 48,
                child: ElevatedButton(
                  onPressed: hasSelection && !_isSyncing
                      ? () async {
                          // Set syncing state
                          setState(() {
                            _isSyncing = true;
                          });

                          // Force sync any pending quantity updates before checkout
                          await ref
                              .read(cartProvider.notifier)
                              .forceSyncPendingUpdates();

                          // Reset syncing state
                          if (mounted) {
                            setState(() {
                              _isSyncing = false;
                            });

                            // Get selected CartItemData objects
                            // Pass complete CartItemData to preserve conversion calculations
                            final selectedCartItems = cartState.items
                                .where((item) =>
                                    cartState.selectedItems[item.product.productId] == true)
                                .toList();

                            // Navigate to checkout with CartItemData
                            // Checkout page will fetch fresh pricing from API
                            context.push(
                              RouteNames.checkout,
                              extra: {'cartItems': selectedCartItems},
                            );
                          }
                        }
                      : null,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: colorScheme.primary,
                    disabledBackgroundColor: colorScheme.inverseSurface.withValues(alpha: 0.6),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                    elevation: 0,
                  ),
                  child: _isSyncing
                      ? CustomLoadingIndicator(
                          color: colorScheme.surface,
                          size: 20,
                        )
                      : Text(
                          'Tiến hành đặt hàng',
                          style: AppTypography.labelLarge.copyWith(
                            color: colorScheme.surface,
                            fontWeight: FontWeight.w600,
                            fontSize: 16,
                          ),
                        ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// Build error banner (shown at top when there's an error but cart has items)
  Widget _buildErrorBanner(String errorMessage) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      color: AppColors.danger.withValues(alpha: 0.1),
      child: Row(
        children: [
          const FaIcon(FontAwesomeIcons.circleExclamation, color: AppColors.danger, size: 18),
          const SizedBox(width: 8),
          Expanded(
            child: Text(
              errorMessage,
              style: AppTypography.bodySmall.copyWith(color: AppColors.danger),
            ),
          ),
        ],
      ),
    );
  }

  /// Build error state (shown when cart fails to load and is empty)
  Widget _buildErrorState(BuildContext context, String errorMessage) {
    final colorScheme = Theme.of(context).colorScheme;

    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const FaIcon(FontAwesomeIcons.circleExclamation, size: 56, color: AppColors.danger),
          const SizedBox(height: 16),
          const Text(
            'Không thể tải giỏ hàng',
            style: AppTypography.headlineMedium,
          ),
          const SizedBox(height: 8),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 32),
            child: Text(
              errorMessage,
              style: AppTypography.bodyMedium.copyWith(color: colorScheme.onSurfaceVariant),
              textAlign: TextAlign.center,
            ),
          ),
          const SizedBox(height: 24),
          ElevatedButton.icon(
            onPressed: () {
              ref.read(cartProvider.notifier).initialize();
            },
            icon: const FaIcon(FontAwesomeIcons.arrowsRotate, size: 20),
            label: const Text('Thử lại'),
          ),
        ],
      ),
    );
  }

  /// Build empty cart state
  Widget _buildEmptyCart(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            FontAwesomeIcons.cartShopping,
            size: 80,
            color: colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
          ),
          const SizedBox(height: 16),
          Text(
            'Giỏ hàng trống',
            style: AppTypography.headlineMedium.copyWith(
              color: colorScheme.onSurfaceVariant,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            'Hãy thêm sản phẩm vào giỏ hàng',
            style: AppTypography.bodyMedium.copyWith(color: colorScheme.onSurfaceVariant),
          ),
          const SizedBox(height: 24),
          ElevatedButton.icon(
            onPressed: () => context.push(RouteNames.products),
            icon: const FaIcon(FontAwesomeIcons.bagShopping, size: 20),
            label: const Text('Xem sản phẩm'),
          ),
        ],
      ),
    );
  }

  /// Show delete confirmation dialog
  void _showDeleteConfirmation(
    BuildContext context,
    WidgetRef ref,
    CartState cartState,
  ) {
    showDialog<void>(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('Xóa sản phẩm'),
        content: Text(
          'Bạn có chắc muốn xóa ${cartState.selectedCount} sản phẩm đã chọn?',
        ),
        actions: [
          TextButton(
            onPressed: () => context.pop(),
            child: const Text('Hủy'),
          ),
          ElevatedButton(
            onPressed: () {
              // Log remove from cart analytics for selected items
              for (final item in cartState.items) {
                if (cartState.selectedItems[item.product.productId] == true) {
                  AnalyticsService.logRemoveFromCart(
                    productId: item.product.productId,
                    productName: item.product.name,
                    price: item.product.basePrice,
                    quantity: item.quantity.toInt(),
                  );
                }
              }
              ref.read(cartProvider.notifier).deleteSelected();
              context.pop();
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                  content: Text('Đã xóa sản phẩm khỏi giỏ hàng'),
                  backgroundColor: AppColors.success,
                  duration: Duration(seconds: 2),
                ),
              );
            },
            style: ElevatedButton.styleFrom(backgroundColor: AppColors.danger),
            child: const Text('Xóa'),
          ),
        ],
      ),
    );
  }
}

/// Custom Checkbox Widget
///
/// Matches HTML design with 22px size, 6px radius, blue when checked.
class _CustomCheckbox extends StatelessWidget {
  const _CustomCheckbox({required this.value, this.onChanged});

  final bool value;
  final ValueChanged<bool?>? onChanged;

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return GestureDetector(
      onTap: () => onChanged?.call(!value),
      child: Container(
        width: 22,
        height: 22,
        decoration: BoxDecoration(
          color: value ? colorScheme.primary : colorScheme.surface,
          border: Border.all(
            color: value ? colorScheme.primary : colorScheme.outlineVariant,
            width: 2,
          ),
          borderRadius: BorderRadius.circular(6),
        ),
        child: value
            ? Icon(
                FontAwesomeIcons.check,
                size: 16,
                color: colorScheme.surface,
              )
            : null,
      ),
    );
  }
}
