import 'package:flutter/material.dart';
import '../../models/user_balance.dart';

class BalancesTab extends StatelessWidget {
  final List<UserBalance> balances;

  const BalancesTab({
    super.key,
    required this.balances,
  });

  @override
  Widget build(BuildContext context) {
    if (balances.isEmpty) {
      return const Center(
        child: Text('Aucune balance à afficher'),
      );
    }

    return ListView.builder(
      padding: const EdgeInsets.all(16),
      itemCount: balances.length,
      itemBuilder: (context, index) {
        final balance = balances[index];
        return _buildBalanceCard(context, balance);
      },
    );
  }

  Widget _buildBalanceCard(BuildContext context, UserBalance balance) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    
    Color balanceColor;
    IconData balanceIcon;
    String balanceText;

    if (balance.shouldReceive) {
      balanceColor = Colors.green;
      balanceIcon = Icons.arrow_downward;
      balanceText = 'À recevoir';
    } else if (balance.shouldPay) {
      balanceColor = Colors.red;
      balanceIcon = Icons.arrow_upward;
      balanceText = 'À payer';
    } else {
      balanceColor = Colors.grey;
      balanceIcon = Icons.check_circle;
      balanceText = 'Équilibré';
    }

    return Card(
      margin: const EdgeInsets.only(bottom: 12),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Row(
          children: [
            CircleAvatar(
              radius: 24,
              backgroundColor: isDark ? Colors.grey[800] : Colors.grey[200],
              child: Text(
                balance.userName.isNotEmpty
                    ? balance.userName[0].toUpperCase()
                    : '?',
                style: const TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    balance.userName,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    'Payé: ${balance.totalPaid.toStringAsFixed(2)} € • Doit: ${balance.totalOwed.toStringAsFixed(2)} €',
                    style: TextStyle(
                      fontSize: 12,
                      color: isDark ? Colors.grey[400] : Colors.grey[600],
                    ),
                  ),
                ],
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Row(
                  children: [
                    Icon(balanceIcon, size: 16, color: balanceColor),
                    const SizedBox(width: 4),
                    Text(
                      '${balance.absoluteBalance.toStringAsFixed(2)} €',
                      style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                        color: balanceColor,
                      ),
                    ),
                  ],
                ),
                Text(
                  balanceText,
                  style: TextStyle(
                    fontSize: 12,
                    color: balanceColor,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
