import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_service/controller/mainController/main_controller.dart';
import 'package:siro_service/views/widgets/my_scafold.dart';

class PassengersPage extends StatelessWidget {
  PassengersPage({super.key});
  final MainController mainController = MainController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      builder: (mainController) {
        final data = _extractPassengerData(mainController);

        return MyScaffold(
          title: data != null
              ? '${data['first_name'] ?? ''} ${data['last_name'] ?? ''}'
              : 'Passenger Not Found'.tr,
          isleading: true,
          body: [
            if (data != null)
              ListView(
                children: [
                  _buildPersonalInfoCard(data),
                  _buildLatestRideCard(data),
                  _buildWalletInfoCard(data),
                ],
              )
            else
              const Center(
                child: Padding(
                  padding: EdgeInsets.all(24.0),
                  child: Text(
                    'No passenger data available.',
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }

  /// Helper method to extract the passenger data safely.
  Map? _extractPassengerData(MainController controller) {
    final passengerData = controller.passengerData;
    if (passengerData == null ||
        passengerData['message'] == null ||
        passengerData['message'].isEmpty) {
      return null;
    }
    return passengerData['message'][0];
  }

  Widget _buildPersonalInfoCard(Map data) {
    return _buildCard(
      title: 'Personal Information'.tr,
      children: [
        _buildInfoRow('Phone'.tr, data['phone']),
        _buildInfoRow('Email'.tr, data['email']),
        _buildInfoRow('Gender'.tr, data['gender']),
        _buildInfoRow('Birthdate'.tr, data['birthdate']),
        // _buildInfoRow('Education'.tr, data['education']),
        _buildInfoRow('Employment'.tr, data['employmentType']),
        _buildInfoRow('Marital Status'.tr, data['maritalStatus']),
      ],
    );
  }

  Widget _buildLatestRideCard(Map data) {
    return _buildCard(
      title: 'Latest Ride'.tr,
      children: [
        _buildInfoRow('Ride ID'.tr, data['ride_id']),
        _buildInfoRow('Date'.tr, data['ride_date']),
        _buildInfoRow('Start Time'.tr, data['ride_time']),
        _buildInfoRow('End Time'.tr, data['ride_endtime']),
        _buildInfoRow(
            'Price'.tr, data['price'] != null ? '\$${data['price']}' : null),
        _buildInfoRow('Status'.tr, data['ride_status']),
        _buildInfoRow('Payment Method'.tr, data['ride_payment_method']),
        _buildInfoRow('Car Type'.tr, data['car_type']),
        _buildInfoRow(
            'Distance'.tr,
            data['distance'] != null
                ? '${(data['distance'] as num).toStringAsFixed(2)} km'
                : null),
      ],
    );
  }

  Widget _buildWalletInfoCard(Map data) {
    return _buildCard(
      title: 'Wallet Information'.tr,
      children: [
        _buildInfoRow(
            'Wallet Balance'.tr,
            data['passenger_wallet_balance'] != null
                ? '\$${data['passenger_wallet_balance']}'
                : null),
        _buildInfoRow(
            'Last Payment Amount'.tr,
            data['passenger_payment_amount'] != null
                ? '\$${data['passenger_payment_amount']}'
                : null),
        _buildInfoRow(
            'Last Payment Method'.tr, data['passenger_payment_method']),
      ],
    );
  }

  Widget _buildCard({required String title, required List<Widget> children}) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title,
                style:
                    const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 16),
            ...children,
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, dynamic value) {
    final displayValue = value?.toString() ?? 'N/A';
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
          Flexible(child: Text(displayValue, overflow: TextOverflow.ellipsis)),
        ],
      ),
    );
  }
}
