import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:service/controller/mainController/main_controller.dart';
import 'package: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) {
      Map data = mainController.passengerData['message'][0];
      return MyScaffold(
        title: (data['first_name']?.toString() ?? '') +
            ' ' +
            (data['last_name']?.toString() ?? ''),
        isleading: true,
        body: [
          ListView(
            children: [
              _buildPersonalInfoCard(data),
              _buildLatestRideCard(data),
              _buildWalletInfoCard(data),
            ],
          ),
        ],
      );
    });
  }

  Widget _buildPersonalInfoCard(Map data) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Personal Information'.tr,
                style:
                    const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 16),
            _buildInfoRow('Phone'.tr, data['phone']?.toString() ?? 'N/A'),
            _buildInfoRow('Email'.tr, data['email']?.toString() ?? 'N/A'),
            _buildInfoRow('Gender'.tr, data['gender']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Birthdate'.tr, data['birthdate']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Education'.tr, data['education']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Employment'.tr, data['employmentType']?.toString() ?? 'N/A'),
            _buildInfoRow('Marital Status'.tr,
                data['maritalStatus']?.toString() ?? 'N/A'),
          ],
        ),
      ),
    );
  }

  Widget _buildLatestRideCard(Map data) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Latest Ride'.tr,
                style:
                    const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 16),
            _buildInfoRow('Ride ID'.tr, data['ride_id']?.toString() ?? 'N/A'),
            _buildInfoRow('Date'.tr, data['ride_date']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Start Time'.tr, data['ride_time']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'End Time'.tr, data['ride_endtime']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Price'.tr, '\$${data['price']?.toString() ?? 'N/A'}'),
            _buildInfoRow(
                'Status'.tr, data['ride_status']?.toString() ?? 'N/A'),
            _buildInfoRow('Payment Method'.tr,
                data['ride_payment_method']?.toString() ?? 'N/A'),
            _buildInfoRow('Car Type'.tr, data['car_type']?.toString() ?? 'N/A'),
            _buildInfoRow(
                'Distance'.tr,
                data['distance'] != null
                    ? '${data['distance'].toStringAsFixed(2)} km'
                    : 'N/A'),
          ],
        ),
      ),
    );
  }

  Widget _buildWalletInfoCard(Map data) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Wallet Information'.tr,
                style:
                    const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 16),
            _buildInfoRow('Wallet Balance'.tr,
                '\$${data['passenger_wallet_balance']?.toString() ?? 'N/A'}'),
            _buildInfoRow('Last Payment Amount'.tr,
                '\$${data['passenger_payment_amount']?.toString() ?? 'N/A'}'),
            _buildInfoRow('Last Payment Method'.tr,
                data['passenger_payment_method']?.toString() ?? 'N/A'),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
          Text(value),
        ],
      ),
    );
  }
}
