import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:siro_driver/constant/finance_design_system.dart';
import 'package:siro_driver/views/widgets/mycircular.dart';
import '../../../controller/payment/driver_payment_controller.dart';
import 'widgets/transaction_preview_item.dart';

class PaymentHistoryDriverPage extends StatelessWidget {
  const PaymentHistoryDriverPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(DriverWalletHistoryController());

    return Scaffold(
      backgroundColor: FinanceDesignSystem.backgroundColor,
      appBar: AppBar(
        title: Text('Payment History'.tr,
            style: TextStyle(fontWeight: FontWeight.bold, color: FinanceDesignSystem.primaryDark)),
        backgroundColor: Colors.transparent,
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios_new_rounded, color: FinanceDesignSystem.primaryDark, size: 20),
          onPressed: () => Get.back(),
        ),
      ),
      body: GetBuilder<DriverWalletHistoryController>(
        builder: (controller) {
          if (controller.isLoading) {
            return const Center(child: MyCircularProgressIndicator());
          }

          if (controller.archive.isEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.history_rounded, size: 80, color: Colors.grey.shade300),
                  const SizedBox(height: 16),
                  Text('No transactions yet'.tr,
                      style: TextStyle(color: Colors.grey.shade400, fontWeight: FontWeight.bold)),
                ],
              ),
            );
          }

          return AnimationLimiter(
            child: ListView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              itemCount: controller.archive.length,
              itemBuilder: (BuildContext context, int index) {
                final tx = controller.archive[index];
                final double amount = double.tryParse(tx['amount']?.toString() ?? '0') ?? 0;
                
                return AnimationConfiguration.staggeredList(
                  position: index,
                  duration: const Duration(milliseconds: 375),
                  child: SlideAnimation(
                    verticalOffset: 50.0,
                    child: FadeInAnimation(
                      child: TransactionPreviewItem(
                        title: amount >= 0 ? 'Credit'.tr : 'Debit'.tr,
                        subtitle: tx['created_at'] ?? '',
                        amount: amount.abs().toStringAsFixed(0),
                        date: tx['created_at']?.split(' ')[0] ?? '',
                        type: amount >= 0 ? 'credit' : 'debit',
                        onTap: () {},
                      ),
                    ),
                  ),
                );
              },
            ),
          );
        },
      ),
    );
  }
}
