import 'package:flutter/material.dart';
import 'package:kmobile/data/models/user.dart';
import 'package:kmobile/features/fund_transfer/screens/fund_transfer_self_amount_screen.dart';
import 'package:kmobile/l10n/app_localizations.dart';
import 'package:kmobile/widgets/bank_logos.dart';

class FundTransferSelfAccountsScreen extends StatelessWidget {
  final String debitAccountNo;
  final String remitterName;
  final List<User> accounts;

  const FundTransferSelfAccountsScreen({
    super.key,
    required this.debitAccountNo,
    required this.remitterName,
    required this.accounts,
  });

  // Helper function to get the full account type name from the short code
  String _getFullAccountType(String? accountType) {
    if (accountType == null || accountType.isEmpty) return 'N/A';
    switch (accountType.toLowerCase()) {
      case 'sa':
      case 'sb':
        return "Savings Account";
      case 'ln':
        return "Loan Account";
      case 'td':
        return "Term Deposit";
      case 'rd':
        return "Recurring Deposit";
      case 'ca':
        return "Current Account";
      default:
        return "Unknown Account";
    }
  }

  @override
  Widget build(BuildContext context) {
    // Filter out the account from which the transfer is being made
    final filteredAccounts =
        accounts.where((acc) => acc.accountNo != debitAccountNo).toList();

    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).selectAccount),
      ),
      body: Stack(
        children: [
          filteredAccounts.isEmpty
              ? const Center(
                  child: Text("No other accounts found"),
                )
              : ListView.builder(
                  itemCount: filteredAccounts.length,
                  itemBuilder: (context, index) {
                    final account = filteredAccounts[index];
                    return Card(
                      margin: const EdgeInsets.symmetric(
                          horizontal: 12, vertical: 6),
                      child: ListTile(
                        leading: CircleAvatar(
                          radius: 24,
                          backgroundColor: Colors.transparent,
                          child: getBankLogo(
                              'Kangra Central Co-operative Bank', context),
                        ),
                        title: Text(account.name ?? 'N/A'),
                        subtitle: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(account.accountNo ?? 'N/A'),
                            Text(
                              _getFullAccountType(account.accountType),
                              style: TextStyle(
                                  fontSize: 12, color: Colors.grey[600]),
                            ),
                          ],
                        ),
                        onTap: () {
                          // Navigate to the amount screen, passing the selected User object directly.
                          // No Beneficiary object is created.
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) =>
                                  FundTransferSelfAmountScreen(
                                debitAccountNo: debitAccountNo,
                                creditAccount: account, // Pass the User object
                                remitterName: remitterName,
                              ),
                            ),
                          );
                        },
                      ),
                    );
                  },
                ),
          IgnorePointer(
            child: Center(
              child: Opacity(
                opacity: 0.07, // Reduced opacity
                child: ClipOval(
                  child: Image.asset(
                    'assets/images/logo.png',
                    width: 200, // Adjust size as needed
                    height: 200, // Adjust size as needed
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
