import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:qpay/screens/groups/models/group_batch_model.dart';
import 'package:intl/intl.dart';
import 'package:qpay/screens/groups/models/recipient_group_model.dart';
import 'package:qpay/models/responsive_policy.dart';
import 'package:qpay/screens/groups/controllers/batch_controller.dart';
import 'package:qpay/screens/groups/controllers/group_controller.dart';
import 'package:qpay/screens/recipient/recipients_controller.dart';
import 'package:qpay/widgets/status_chip_widget.dart';
import 'package:skeletonizer/skeletonizer.dart';

class GroupDetailScreen extends StatefulWidget {
  final RecipientGroup group;

  const GroupDetailScreen({super.key, required this.group});

  @override
  State<GroupDetailScreen> createState() => _GroupDetailScreenState();
}

class _GroupDetailScreenState extends State<GroupDetailScreen>
    with TickerProviderStateMixin {
  late GroupController groupController;
  late BatchController batchController;
  final _searchController = TextEditingController();
  final _scrollController = ScrollController();
  final _memberSearchController = TextEditingController();
  late final AnimationController _membersAnimController;
  String _memberQuery = '';
  String? _statusFilter;
  String? _currencyFilter;

  static const _statusFilterItems = <DropdownMenuItem<String?>>[
    DropdownMenuItem(value: null, child: Text('All')),
    DropdownMenuItem(value: 'CREATED', child: Text('Created')),
    DropdownMenuItem(value: 'CONFIRMING', child: Text('Confirming')),
    DropdownMenuItem(value: 'CONFIRMED_ALL', child: Text('Confirmed All')),
    DropdownMenuItem(value: 'REQUESTED', child: Text('Requested')),
    DropdownMenuItem(value: 'COMPLETED', child: Text('Completed')),
    DropdownMenuItem(value: 'POLL_FAILED', child: Text('Poll Failed')),
    DropdownMenuItem(value: 'CONFIRM_FAILED', child: Text('Confirm Failed')),
    DropdownMenuItem(value: 'FAILED', child: Text('Failed')),
  ];

  static const _currencyFilterItems = <DropdownMenuItem<String?>>[
    DropdownMenuItem(value: null, child: Text('All')),
    DropdownMenuItem(value: 'USD', child: Text('USD')),
    DropdownMenuItem(value: 'ZWL', child: Text('ZWL')),
    DropdownMenuItem(value: 'ZAR', child: Text('ZAR')),
  ];

  @override
  void initState() {
    super.initState();
    groupController = GroupController(context);
    batchController = BatchController(context);
    _loadData();
    _scrollController.addListener(_onScroll);
    _searchController.addListener(() => setState(() {}));
    _membersAnimController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 600),
    )..forward();
  }

  void _onScroll() {
    if (_scrollController.position.pixels >=
        _scrollController.position.maxScrollExtent - 200) {
      batchController.loadNextPage(widget.group.id ?? '');
    }
  }

  Future<void> _loadData() async {
    final groupId = widget.group.id ?? '';
    await Future.wait([
      batchController.listBatches(groupId),
      groupController.listMembers(groupId),
    ]);
  }

  void _onSearchChanged(String value) {
    batchController.searchBatches(
      widget.group.id ?? '',
      search: value.isNotEmpty ? value : null,
      status: _statusFilter,
      currency: _currencyFilter,
    );
  }

  void _showMembersSheet() {
    _memberSearchController.clear();
    _memberQuery = '';
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Theme.of(context).colorScheme.surface,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
      ),
      builder: (ctx) {
        return DraggableScrollableSheet(
          initialChildSize: 0.7,
          minChildSize: 0.4,
          maxChildSize: 0.92,
          expand: false,
          builder: (context, scrollController) {
            return StatefulBuilder(
              builder: (context, setSheetState) {
                return Column(
                  children: [
                    _buildMembersSheetHandle(),
                    _buildMembersSheetHeader(setSheetState, scrollController),
                    Expanded(
                      child: ListenableBuilder(
                        listenable: groupController,
                        builder: (context, _) {
                          final allMembers = groupController.model.members;
                          final filtered = _filteredMembers(allMembers);
                          if (groupController.model.isLoading) {
                            return _buildMembersSkeletonList(scrollController);
                          }
                          if (allMembers.isEmpty) {
                            return _buildMembersEmptyState();
                          }
                          if (filtered.isEmpty) {
                            return _buildNoSearchResultsState();
                          }
                          return ListView.separated(
                            controller: scrollController,
                            padding: const EdgeInsets.fromLTRB(16, 8, 16, 24),
                            itemCount: filtered.length,
                            separatorBuilder: (_, __) =>
                                const SizedBox(height: 10),
                            itemBuilder: (context, index) {
                              return _buildAnimatedMemberCard(
                                index,
                                filtered[index],
                              );
                            },
                          );
                        },
                      ),
                    ),
                  ],
                );
              },
            );
          },
        );
      },
    );
  }

  Widget _buildMembersSheetHandle() {
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 6),
      child: Container(
        width: 44,
        height: 5,
        decoration: BoxDecoration(
          color: Colors.grey.shade300,
          borderRadius: BorderRadius.circular(10),
        ),
      ),
    );
  }

  Widget _buildMembersSheetHeader(
    StateSetter setSheetState,
    ScrollController scrollController,
  ) {
    final total = groupController.model.members.length;
    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 4, 12, 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: Theme.of(
                    context,
                  ).colorScheme.primary.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(
                  Icons.people_alt_outlined,
                  color: Theme.of(context).colorScheme.primary,
                  size: 22,
                ),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      'Members',
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.w700,
                        color: Colors.black87,
                      ),
                    ),
                    const SizedBox(height: 2),
                    Text(
                      '$total ${total == 1 ? "member" : "members"} in this group',
                      style: TextStyle(
                        fontSize: 13,
                        color: Colors.grey.shade600,
                      ),
                    ),
                  ],
                ),
              ),
              IconButton(
                icon: Icon(Icons.close, color: Colors.grey.shade700),
                tooltip: 'Close',
                onPressed: () => Navigator.of(context).pop(),
              ),
            ],
          ),
          if (total > 0) ...[
            const SizedBox(height: 14),
            TextFormField(
              controller: _memberSearchController,
              onChanged: (v) {
                setSheetState(() => _memberQuery = v);
              },
              decoration: InputDecoration(
                hintText: 'Search members...',
                hintStyle: TextStyle(color: Colors.grey.shade500, fontSize: 14),
                prefixIcon: Icon(
                  Icons.search,
                  color: Colors.grey.shade600,
                  size: 20,
                ),
                suffixIcon: _memberQuery.isNotEmpty
                    ? IconButton(
                        icon: Icon(
                          Icons.clear,
                          color: Colors.grey.shade600,
                          size: 18,
                        ),
                        onPressed: () {
                          _memberSearchController.clear();
                          setSheetState(() => _memberQuery = '');
                        },
                      )
                    : null,
                isDense: true,
                contentPadding: const EdgeInsets.symmetric(
                  horizontal: 12,
                  vertical: 12,
                ),
                filled: true,
                fillColor: Colors.grey.shade100,
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                  borderSide: BorderSide.none,
                ),
                focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                  borderSide: BorderSide(
                    color: Theme.of(context).colorScheme.primary,
                  ),
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }

  List<RecipientGroupMember> _filteredMembers(
    List<RecipientGroupMember> members,
  ) {
    if (_memberQuery.trim().isEmpty) return members;
    final q = _memberQuery.toLowerCase().trim();
    return members.where((m) {
      final r = m.recipient;
      return (r.name?.toLowerCase().contains(q) ?? false) ||
          (r.account?.toLowerCase().contains(q) ?? false) ||
          (r.email?.toLowerCase().contains(q) ?? false) ||
          (r.phoneNumber?.toLowerCase().contains(q) ?? false) ||
          (r.latestProviderLabel?.toLowerCase().contains(q) ?? false);
    }).toList();
  }

  Widget _buildAnimatedMemberCard(int index, RecipientGroupMember member) {
    final clampedIndex = index.clamp(0, 5);
    final animation =
        Tween<Offset>(begin: const Offset(0, 0.06), end: Offset.zero).animate(
          CurvedAnimation(
            parent: _membersAnimController,
            curve: Interval(
              0.05 * clampedIndex,
              1.0,
              curve: Curves.easeOutCubic,
            ),
          ),
        );
    return FadeTransition(
      opacity: _membersAnimController,
      child: SlideTransition(
        position: animation,
        child: _buildMemberCard(member),
      ),
    );
  }

  Widget _buildMemberCard(RecipientGroupMember member) {
    final recipient = member.recipient;
    final name = recipient.name?.isNotEmpty == true
        ? recipient.name!
        : 'Unnamed recipient';
    final initials = _initialsFor(name);
    return Material(
      color: Colors.transparent,
      child: InkWell(
        borderRadius: BorderRadius.circular(16),
        onTap: () {
          Navigator.of(context).pop();
        },
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(16),
            border: Border.all(color: Colors.grey.shade200, width: 1),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withValues(alpha: 0.04),
                blurRadius: 8,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 14),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildMemberAvatar(initials),
                const SizedBox(width: 14),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        name,
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: Colors.black87,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      const SizedBox(height: 6),
                      _buildMemberSubtitleRow(recipient, member.account),
                      if (recipient.phoneNumber != null &&
                          recipient.phoneNumber!.isNotEmpty) ...[
                        const SizedBox(height: 6),
                        _buildContactLine(
                          Icons.phone_outlined,
                          recipient.phoneNumber!,
                        ),
                      ],
                      if (recipient.email != null &&
                          recipient.email!.isNotEmpty) ...[
                        const SizedBox(height: 4),
                        _buildContactLine(
                          Icons.email_outlined,
                          recipient.email!,
                        ),
                      ],
                    ],
                  ),
                ),
                _buildMemberPopupMenu(member),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildMemberAvatar(String initials) {
    return Container(
      width: 48,
      height: 48,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Theme.of(context).colorScheme.primary,
            Theme.of(context).colorScheme.primary.withValues(alpha: 0.7),
          ],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        shape: BoxShape.circle,
        boxShadow: [
          BoxShadow(
            color: Theme.of(
              context,
            ).colorScheme.primary.withValues(alpha: 0.25),
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      alignment: Alignment.center,
      child: Text(
        initials,
        style: const TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.bold,
          color: Colors.white,
          letterSpacing: 0.5,
        ),
      ),
    );
  }

  Widget _buildMemberSubtitleRow(Recipient recipient, String? memberAccount) {
    final account = memberAccount?.isNotEmpty == true
        ? memberAccount
        : recipient.account;
    final hasAccount = account != null && account.isNotEmpty;
    final hasProvider =
        recipient.latestProviderLabel != null &&
        recipient.latestProviderLabel!.isNotEmpty;
    if (!hasAccount && !hasProvider) {
      return const SizedBox.shrink();
    }
    return Wrap(
      crossAxisAlignment: WrapCrossAlignment.center,
      spacing: 6,
      runSpacing: 4,
      children: [
        if (hasProvider)
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
            decoration: BoxDecoration(
              color: Theme.of(
                context,
              ).colorScheme.primary.withValues(alpha: 0.08),
              borderRadius: BorderRadius.circular(6),
            ),
            child: Text(
              recipient.latestProviderLabel!,
              style: TextStyle(
                fontSize: 11,
                fontWeight: FontWeight.w600,
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
          ),
        if (hasAccount)
          Text(
            account!,
            style: TextStyle(
              fontSize: 13,
              color: Colors.grey.shade700,
              fontWeight: FontWeight.w500,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
      ],
    );
  }

  Widget _buildContactLine(IconData icon, String text) {
    return Row(
      children: [
        Icon(icon, size: 13, color: Colors.grey.shade500),
        const SizedBox(width: 4),
        Flexible(
          child: Text(
            text,
            style: TextStyle(fontSize: 12, color: Colors.grey.shade600),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }

  Widget _buildMemberPopupMenu(RecipientGroupMember member) {
    return PopupMenuButton<String>(
      icon: Icon(Icons.more_vert, color: Colors.grey.shade500, size: 20),
      padding: EdgeInsets.zero,
      splashRadius: 20,
      tooltip: 'More options',
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      onSelected: (value) async {
        if (value == 'remove') {
          await _confirmRemoveMember(member);
        }
      },
      itemBuilder: (context) => [
        const PopupMenuItem(
          value: 'remove',
          child: Row(
            children: [
              Icon(Icons.person_remove_outlined, size: 18, color: Colors.red),
              SizedBox(width: 10),
              Text(
                'Remove from group',
                style: TextStyle(
                  color: Colors.red,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Future<void> _confirmRemoveMember(RecipientGroupMember member) async {
    final name = member.recipient.name ?? 'this member';
    final confirmed = await showDialog<bool>(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        title: const Text('Remove member'),
        content: Text('Are you sure you want to remove $name from this group?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(ctx).pop(false),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () => Navigator.of(ctx).pop(true),
            style: TextButton.styleFrom(foregroundColor: Colors.red),
            child: const Text('Remove'),
          ),
        ],
      ),
    );
    if (confirmed == true && mounted) {
      await groupController.removeMember(
        groupId: widget.group.id ?? '',
        recipientId: member.id ?? '',
        workspaceId: widget.group.workspaceId ?? '',
      );
    }
  }

  Widget _buildMembersSkeletonList(ScrollController scrollController) {
    return ListView.separated(
      controller: scrollController,
      padding: const EdgeInsets.fromLTRB(16, 8, 16, 24),
      itemCount: 6,
      separatorBuilder: (_, __) => const SizedBox(height: 10),
      itemBuilder: (context, index) {
        return Skeletonizer(
          enabled: true,
          child: _buildMemberCard(
            RecipientGroupMember(
              recipientGroupId: '',
              recipient: Recipient(
                name: 'Loading Name Here',
                account: '0000000000',
                phoneNumber: '+000 000 0000',
                email: 'loading@example.com',
                latestProviderLabel: 'PROVIDER',
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildMembersEmptyState() {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(32),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              width: 88,
              height: 88,
              decoration: BoxDecoration(
                color: Theme.of(
                  context,
                ).colorScheme.primary.withValues(alpha: 0.08),
                shape: BoxShape.circle,
              ),
              child: Icon(
                Icons.people_alt_outlined,
                size: 44,
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
            const SizedBox(height: 20),
            const Text(
              'No members yet',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w700,
                color: Colors.black87,
              ),
            ),
            const SizedBox(height: 8),
            Text(
              'Members added to this group will appear\nhere once they are available.',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14,
                color: Colors.grey.shade600,
                height: 1.4,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildNoSearchResultsState() {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(32),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              Icons.search_off_outlined,
              size: 56,
              color: Colors.grey.shade400,
            ),
            const SizedBox(height: 16),
            const Text(
              'No matching members',
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Colors.black87,
              ),
            ),
            const SizedBox(height: 6),
            Text(
              'No results for "$_memberQuery"',
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 13, color: Colors.grey.shade600),
            ),
          ],
        ),
      ),
    );
  }

  String _initialsFor(String name) {
    final cleaned = name.trim();
    if (cleaned.isEmpty) return '?';
    final parts = cleaned.split(RegExp(r'\s+'));
    if (parts.length == 1) {
      return parts.first.characters.first.toUpperCase();
    }
    return (parts.first.characters.first + parts.last.characters.first)
        .toUpperCase();
  }

  void _onDeleteSelected() async {
    final workspaceId = widget.group.workspaceId ?? '';
    final count = batchController.model.selectedBatchIds.length;

    final confirmed = await showDialog<bool>(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text('Delete batches'),
        content: Text('Are you sure you want to delete $count batch(es)?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(ctx).pop(false),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () => Navigator.of(ctx).pop(true),
            style: TextButton.styleFrom(foregroundColor: Colors.red),
            child: const Text('Delete'),
          ),
        ],
      ),
    );

    if (confirmed == true) {
      await batchController.deleteSelectedBatches(
        groupId: widget.group.id ?? '',
        workspaceId: workspaceId,
      );
    }
  }

  void _showFilterSheet() {
    showModalBottomSheet(
      context: context,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
      ),
      builder: (ctx) {
        return StatefulBuilder(
          builder: (context, setSheetState) {
            return Padding(
              padding: const EdgeInsets.all(20),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      const Text(
                        'Filter Batches',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      const Spacer(),
                      TextButton(
                        onPressed: () {
                          setSheetState(() {
                            _statusFilter = null;
                            _currencyFilter = null;
                          });
                        },
                        child: const Text('Clear All'),
                      ),
                    ],
                  ),
                  const SizedBox(height: 16),
                  DropdownButtonFormField<String?>(
                    initialValue: _statusFilter,
                    decoration: InputDecoration(
                      labelText: 'Status',
                      filled: true,
                      fillColor: Colors.grey.shade100,
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide.none,
                      ),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 16,
                        vertical: 14,
                      ),
                    ),
                    items: _statusFilterItems,
                    onChanged: (v) {
                      setSheetState(() => _statusFilter = v);
                    },
                  ),
                  const SizedBox(height: 16),
                  DropdownButtonFormField<String?>(
                    initialValue: _currencyFilter,
                    decoration: InputDecoration(
                      labelText: 'Currency',
                      filled: true,
                      fillColor: Colors.grey.shade100,
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide.none,
                      ),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 16,
                        vertical: 14,
                      ),
                    ),
                    items: _currencyFilterItems,
                    onChanged: (v) {
                      setSheetState(() => _currencyFilter = v);
                    },
                  ),
                  const SizedBox(height: 24),
                  SizedBox(
                    width: double.infinity,
                    child: ElevatedButton(
                      onPressed: () => _applyFilters(ctx),
                      child: const Text('Apply Filters'),
                    ),
                  ),
                  const SizedBox(height: 8),
                ],
              ),
            );
          },
        );
      },
    );
  }

  void _applyFilters(BuildContext ctx) {
    Navigator.of(ctx).pop();
    batchController.searchBatches(
      widget.group.id ?? '',
      search: _searchController.text.isNotEmpty ? _searchController.text : null,
      status: _statusFilter,
      currency: _currencyFilter,
    );
  }

  @override
  void dispose() {
    _searchController.dispose();
    _scrollController.dispose();
    _memberSearchController.dispose();
    _membersAnimController.dispose();
    groupController.dispose();
    batchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.group.name),
        centerTitle: true,
        actions: [
          IconButton(
            icon: const Icon(Icons.people_outline),
            tooltip: 'View members',
            onPressed: _showMembersSheet,
          ),
        ],
      ),
      body: ListenableBuilder(
        listenable: batchController,
        builder: (context, _) {
          return Column(
            children: [
              Expanded(
                child: CustomScrollView(
                  controller: _scrollController,
                  slivers: [
                    SliverToBoxAdapter(
                      child: Center(
                        child: LayoutBuilder(
                          builder: (context, constraints) {
                            final width =
                                constraints.maxWidth > ResponsivePolicy.md
                                ? ResponsivePolicy.md.toDouble()
                                : constraints.maxWidth;
                            return SizedBox(
                              width: width,
                              child: Padding(
                                padding: const EdgeInsets.all(16),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    _buildSearchRow(),
                                    const SizedBox(height: 16),
                                    _buildContent(),
                                  ],
                                ),
                              ),
                            );
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              if (batchController.model.selectMode) _buildSelectionBottomBar(),
            ],
          );
        },
      ),
    );
  }

  Widget _buildSearchRow() {
    return Row(
      children: [
        Expanded(
          child: TextFormField(
            controller: _searchController,
            decoration: InputDecoration(
              hintText: 'Search batches by description...',
              prefixIcon: const Icon(Icons.search),
              suffixIcon: _searchController.text.isNotEmpty
                  ? IconButton(
                      icon: const Icon(Icons.clear),
                      onPressed: () {
                        _searchController.clear();
                        _onSearchChanged('');
                      },
                    )
                  : null,
              filled: true,
              fillColor: Colors.grey.shade100,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(12),
                borderSide: BorderSide.none,
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(12),
                borderSide: BorderSide(
                  color: Theme.of(context).colorScheme.primary,
                ),
              ),
            ),
            onChanged: _onSearchChanged,
          ),
        ),
        const SizedBox(width: 8),
        _buildIconButton(
          icon: Icons.filter_list,
          tooltip: 'Filter batches',
          isActive: _statusFilter != null || _currencyFilter != null,
          onPressed: _showFilterSheet,
        ),
        const SizedBox(width: 8),
        _buildIconButton(
          icon: batchController.model.selectMode
              ? Icons.checklist
              : Icons.checklist_outlined,
          tooltip: 'Select batches',
          isActive: batchController.model.selectMode,
          onPressed: () => batchController.toggleSelectMode(),
        ),
        const SizedBox(width: 8),
        _buildIconButton(
          icon: Icons.refresh,
          tooltip: 'Refresh batches',
          isActive: false,
          onPressed: _loadData,
        ),
        const SizedBox(width: 8),
        _buildIconButton(
          icon: Icons.add,
          tooltip: 'Create batch',
          isActive: false,
          onPressed: () async {
            final result = await context.push(
              '/groups/${widget.group.id}/batches/create',
              extra: widget.group,
            );
            if (result == true) {
              _loadData();
            }
          },
        ),
      ],
    );
  }

  Widget _buildIconButton({
    required IconData icon,
    required String tooltip,
    required bool isActive,
    required VoidCallback onPressed,
  }) {
    return Container(
      decoration: BoxDecoration(
        color: isActive
            ? Theme.of(context).colorScheme.primary
            : Colors.grey.shade100,
        borderRadius: BorderRadius.circular(12),
      ),
      child: IconButton(
        icon: Icon(icon, color: isActive ? Colors.white : Colors.grey.shade700),
        tooltip: tooltip,
        onPressed: onPressed,
      ),
    );
  }

  Widget _buildContent() {
    if (batchController.model.isLoading) {
      return const Center(
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 60),
          child: CircularProgressIndicator(),
        ),
      );
    }

    if (batchController.model.batches.isEmpty) {
      return Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 40),
          child: Column(
            children: [
              Icon(
                Icons.receipt_long_outlined,
                size: 64,
                color: Colors.grey.shade400,
              ),
              const SizedBox(height: 16),
              const Text(
                'No batches yet.',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
              ),
              const SizedBox(height: 8),
              Text(
                'Tap + to create a new batch payment.',
                style: TextStyle(fontSize: 14, color: Colors.grey.shade600),
              ),
            ],
          ),
        ),
      );
    }

    return Column(
      children: [
        if (batchController.model.selectMode)
          Padding(
            padding: const EdgeInsets.only(bottom: 8),
            child: Row(
              children: [
                Text(
                  '${batchController.model.selectedBatchIds.length} selected',
                  style: TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w500,
                    color: Colors.grey.shade600,
                  ),
                ),
              ],
            ),
          ),
        if (_statusFilter != null || _currencyFilter != null)
          Padding(
            padding: const EdgeInsets.only(bottom: 8),
            child: Row(
              children: [
                if (_statusFilter != null)
                  _activeFilterChip('Status: $_statusFilter', () {
                    setState(() => _statusFilter = null);
                    _onSearchChanged(_searchController.text);
                  }),
                if (_currencyFilter != null) const SizedBox(width: 6),
                if (_currencyFilter != null)
                  _activeFilterChip('Currency: $_currencyFilter', () {
                    setState(() => _currencyFilter = null);
                    _onSearchChanged(_searchController.text);
                  }),
              ],
            ),
          ),
        ListView.separated(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: batchController.model.batches.length,
          separatorBuilder: (_, __) => const SizedBox(height: 8),
          itemBuilder: (context, index) {
            return _buildBatchCard(
              context,
              batchController.model.batches[index],
              widget.group,
            );
          },
        ),
        if (batchController.model.isLoadingMore)
          const Padding(
            padding: EdgeInsets.symmetric(vertical: 16),
            child: Center(child: CircularProgressIndicator(strokeWidth: 2)),
          ),
      ],
    );
  }

  Widget _activeFilterChip(String label, VoidCallback onRemove) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.primary.withAlpha(20),
        borderRadius: BorderRadius.circular(20),
        border: Border.all(
          color: Theme.of(context).colorScheme.primary.withAlpha(60),
        ),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: 12,
              color: Theme.of(context).colorScheme.primary,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(width: 4),
          InkWell(
            onTap: onRemove,
            borderRadius: BorderRadius.circular(10),
            child: Padding(
              padding: const EdgeInsets.all(2),
              child: Icon(
                Icons.close,
                size: 14,
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBatchCard(
    BuildContext context,
    GroupBatch batch,
    RecipientGroup group,
  ) {
    final isSelected = batchController.model.selectedBatchIds.contains(
      batch.id,
    );
    final inSelectMode = batchController.model.selectMode;

    final status = batch.status ?? 'UNKNOWN';
    final createdAt = batch.createdAt;
    final parsedDate = createdAt != null ? DateTime.tryParse(createdAt) : null;
    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(
          color: inSelectMode && isSelected
              ? Theme.of(context).colorScheme.primary
              : Colors.black12.withAlpha(30),
          width: inSelectMode && isSelected ? 2 : 1,
        ),
      ),
      child: InkWell(
        borderRadius: BorderRadius.circular(12),
        onTap: inSelectMode
            ? () => batchController.toggleBatchSelection(batch.id!)
            : () => context.push('/groups/batches/${batch.id}'),
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Row(
            children: [
              if (inSelectMode)
                Padding(
                  padding: const EdgeInsets.only(right: 8),
                  child: Icon(
                    isSelected
                        ? Icons.check_box
                        : Icons.check_box_outline_blank,
                    size: 20,
                    color: isSelected
                        ? Theme.of(context).colorScheme.primary
                        : Colors.grey.shade400,
                  ),
                ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        if (batch.description != null &&
                            batch.description!.isNotEmpty)
                          Flexible(
                            child: Text(
                              batch.description!,
                              style: const TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.w700,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        const SizedBox(width: 6),
                        StatusChip(status: status),
                      ],
                    ),
                    const SizedBox(height: 4),
                    Text(
                      '${batch.currency ?? 'USD'} ${batch.value?.toStringAsFixed(2) ?? '—'}',
                      style: TextStyle(
                        fontSize: 13,
                        color: Colors.grey.shade600,
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        if (parsedDate != null)
                          Padding(
                            padding: const EdgeInsets.only(top: 6),
                            child: Text(
                              DateFormat.yMMMd().add_jm().format(parsedDate),
                              style: TextStyle(
                                fontSize: 12,
                                color: Colors.grey.shade500,
                              ),
                            ),
                          ),
                        if (batch.count != null)
                          Padding(
                            padding: const EdgeInsets.only(top: 4),
                            child: Row(
                              children: [
                                _statIcon(
                                  Icons.people_outline,
                                  '${batch.count}',
                                ),
                                const SizedBox(width: 4),
                                _statIcon(
                                  Icons.check_circle_outline,
                                  '${batch.successfulCount ?? 0}',
                                ),
                                const SizedBox(width: 4),
                                _statIcon(
                                  Icons.cancel_outlined,
                                  '${batch.failedCount ?? 0}',
                                ),
                              ],
                            ),
                          ),
                      ],
                    ),
                  ],
                ),
              ),
              if (!inSelectMode)
                Icon(Icons.chevron_right, color: Colors.grey.shade400),
            ],
          ),
        ),
      ),
    );
  }

  Widget _statIcon(IconData icon, String label) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 12, color: Colors.grey.shade500),
        const SizedBox(width: 3),
        Text(
          label,
          style: TextStyle(
            fontSize: 11,
            color: Colors.grey.shade600,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget _buildSelectionBottomBar() {
    final selectedCount = batchController.model.selectedBatchIds.length;
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.08),
            blurRadius: 8,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Row(
            children: [
              Text(
                '$selectedCount selected',
                style: const TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                ),
              ),
              const Spacer(),
              TextButton.icon(
                onPressed: selectedCount > 0 ? _onDeleteSelected : null,
                icon: const Icon(Icons.delete_outline, size: 20),
                label: const Text('Delete'),
                style: TextButton.styleFrom(foregroundColor: Colors.red),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
