import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../controller/drivers/driver_not_active_controller.dart';

class DriverDetailsPage extends StatelessWidget {
  final String driverId;
  final DriverController controller = Get.find();

  DriverDetailsPage({super.key, required this.driverId});

  /// Helper function to safely get String values from dynamic map
  String safeVal(Map d, String key) {
    final v = d[key];
    if (v == null || v == false) return '';
    return v.toString();
  }

  @override
  Widget build(BuildContext context) {
    controller.getDriverDetails(driverId);

    return Scaffold(
      appBar: AppBar(title: const Text("Driver Details")),
      body: GetBuilder<DriverController>(
        id: 'driverDetails',
        builder: (c) {
          if (c.driverDetails.isEmpty) {
            return const Center(child: CircularProgressIndicator());
          }

          final d = c.driverDetails['driver'] as Map;
          final docs = c.driverDetails['documents'] as List;

          return SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                    "Name: ${safeVal(d, 'first_name')} ${safeVal(d, 'last_name')}"),
                Text("Phone: ${safeVal(d, 'phone')}"),
                Text("Email: ${safeVal(d, 'email')}"),
                Text("National Number: ${safeVal(d, 'national_number')}"),
                Text("Gender: ${safeVal(d, 'gender')}"),
                Text("Birthdate: ${safeVal(d, 'birthdate')}"),
                Text("Status: ${safeVal(d, 'status')}"),
                Text("License Type: ${safeVal(d, 'license_type')}"),
                Text("License Categories: ${safeVal(d, 'license_categories')}"),
                Text("Issue Date: ${safeVal(d, 'issue_date')}"),
                Text("Expiry Date: ${safeVal(d, 'expiry_date')}"),
                Text("Address: ${safeVal(d, 'address')}"),
                Text("Site: ${safeVal(d, 'site')}"),
                Text("Employment Type: ${safeVal(d, 'employmentType')}"),
                Text("Marital Status: ${safeVal(d, 'maritalStatus')}"),
                const SizedBox(height: 16),
                const Text("Documents:",
                    style: TextStyle(fontWeight: FontWeight.bold)),
                const SizedBox(height: 8),
                ...docs.map((doc) => Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(safeVal(doc, "doc_type")),
                        const SizedBox(height: 4),
                        Image.network(
                          safeVal(doc, "link"),
                          height: 200,
                          fit: BoxFit.contain,
                          errorBuilder: (ctx, err, st) =>
                              const Icon(Icons.broken_image),
                        ),
                        const SizedBox(height: 16),
                      ],
                    )),
              ],
            ),
          );
        },
      ),
    );
  }
}
