import 'package:flutter/material.dart';

import 'package:pshared/models/payment/status.dart';
import 'package:pshared/utils/localization.dart';

import 'package:pweb/models/wallet_transaction.dart';
import 'package:pweb/providers/wallet_transactions.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class WalletHistoryFilters extends StatelessWidget {
  final WalletTransactionsProvider provider;
  final VoidCallback onPickRange;

  const WalletHistoryFilters({
    super.key,
    required this.provider,
    required this.onPickRange,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final loc = AppLocalizations.of(context)!;

    return Card(
      elevation: 2,
      color: theme.colorScheme.onSecondary,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  loc.walletActivity,
                  style: theme.textTheme.titleMedium,
                ),
                if (provider.hasFilters)
                  TextButton(
                    onPressed: provider.resetFilters,
                    child: Text(loc.reset),
                  ),
              ],
            ),
            const SizedBox(height: 12),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: WalletTransactionType.values.map((type) {
                final isSelected = provider.selectedTypes.contains(type);
                return FilterChip(
                  label: Text(type.label(context)),
                  selected: isSelected,
                  onSelected: (_) => provider.toggleType(type),
                  pressElevation: 0,
                );
              }).toList(),
            ),
            const SizedBox(height: 8),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: OperationStatus.values.map((status) {
                final isSelected = provider.selectedStatuses.contains(status);
                return FilterChip(
                  label: Text(status.localized(context)),
                  selected: isSelected,
                  onSelected: (_) => provider.toggleStatus(status),
                  pressElevation: 0,
                );
              }).toList(),
            ),
            const SizedBox(height: 12),
            Align(
              alignment: Alignment.centerRight,
              child: OutlinedButton.icon(
                onPressed: onPickRange,
                icon: const Icon(Icons.date_range_outlined),
                label: Text(
                  provider.dateRange == null
                      ? loc.selectPeriod
                      : '${dateToLocalFormat(context, provider.dateRange!.start)} – ${dateToLocalFormat(context, provider.dateRange!.end)}',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
