import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:service/constant/colors.dart';
import 'package:service/controller/mainController/main_controller.dart';
import 'package:service/views/widgets/my_scafold.dart';
import 'package:service/main.dart';
import 'package:service/constant/box_name.dart';

import 'registration_captain_page.dart';

class DriversCantRegister extends StatelessWidget {
  DriversCantRegister({super.key});
//
  @override
  Widget build(BuildContext context) {
    Get.put(MainController());

    // Unified action button (WhatsApp - Edit - etc)
    Widget buildActionButton({
      required IconData icon,
      required Color color,
      required VoidCallback onPressed,
    }) {
      return CupertinoButton(
        padding: const EdgeInsets.symmetric(horizontal: 6.0),
        onPressed: onPressed,
        child: Icon(icon, color: color, size: 28),
      );
    }

    return MyScaffold(
      title: 'Drivers Want Register'.tr,
      isleading: true,
      body: [
        GetBuilder<MainController>(builder: (mainController) {
          return Column(
            children: [
              // Search
              Padding(
                padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
                child: CupertinoSearchTextField(
                  keyboardType: TextInputType.phone,
                  onChanged: (value) => mainController.searchDrivers(value),
                  placeholder: 'Search by phone number'.tr,
                ),
              ),

              // List
              Expanded(
                child: mainController.filteredDrivers.isEmpty
                    ? Center(
                        child: Text(
                          'No drivers found'.tr,
                          style:
                              TextStyle(color: Colors.grey[600], fontSize: 16),
                        ),
                      )
                    : ListView.builder(
                        itemCount: mainController.filteredDrivers.length,
                        itemBuilder: (context, index) {
                          final driver = mainController.filteredDrivers[index];

                          final notesController =
                              TextEditingController(text: driver['note'] ?? '');

                          return Card(
                            margin: const EdgeInsets.symmetric(
                                horizontal: 12.0, vertical: 8.0),
                            elevation: 3,
                            clipBehavior: Clip.antiAlias,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                              side: BorderSide(
                                color: driver['note'] != null
                                    ? AppColor.secondaryColor
                                    : Colors.transparent,
                                width: 2.5,
                              ),
                            ),
                            child: Padding(
                              padding:
                                  const EdgeInsets.fromLTRB(16, 12, 16, 12),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  // Header
                                  Text(
                                    'Driver Phone: ${driver['first_name'] ?? ''} ${driver['last_name'] ?? ''}',
                                    style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                      color: Colors.black87,
                                    ),
                                  ),

                                  const Divider(height: 20),

                                  // Phone Row
                                  Row(
                                    children: [
                                      const Icon(
                                        CupertinoIcons.phone_fill,
                                        color: AppColor.primaryColor,
                                        size: 22,
                                      ),

                                      const SizedBox(width: 12),

                                      Expanded(
                                        child: InkWell(
                                          onTap: () {
                                            mainController.makePhoneCall(
                                                driver['phone_number']);
                                          },
                                          child: Text(
                                            driver['phone_number'] ?? 'N/A',
                                            style: const TextStyle(
                                              fontSize: 17,
                                              letterSpacing: 1.2,
                                            ),
                                          ),
                                        ),
                                      ),

                                      // WhatsApp button
                                      buildActionButton(
                                        icon: Icons.send,
                                        color: const Color(0xFF25D366),
                                        onPressed: () {
                                          String message = "مرحباً،\n\n"
                                              "يظهر لدينا في نظام تطبيق *انطلق* أنك لم تكمل عملية التسجيل بعد.\n"
                                              "ندعوك لإكمال التسجيل للاستفادة من مزايا التطبيق والبدء بالعمل معنا.\n\n"
                                              "إذا احتجت لأي مساعدة، تواصل معنا على خدمة العملاء:\n"
                                              "+963 952 475 742\n\n"
                                              "+963 952 475 740\n\n"
                                              "فريق انطلق يتمنى لك يوماً سعيداً.";

                                          mainController.launchCommunication(
                                            'whatsapp',
                                            driver['phone_number'],
                                            message,
                                          );
                                        },
                                      ),

                                      // Edit button → go to registration form
                                      buildActionButton(
                                        icon: CupertinoIcons
                                            .pencil_ellipsis_rectangle,
                                        color: AppColor.gold,
                                        onPressed: () {
                                          Get.to(
                                            () => RegisterCaptain(),
                                            arguments: {
                                              "phone": driver['phone_number'],
                                              "driverId": driver['driverId'],
                                            },
                                          );
                                        },
                                      ),
                                      buildActionButton(
                                        icon: CupertinoIcons
                                            .pencil_ellipsis_rectangle,
                                        color: AppColor.redColor,
                                        onPressed: () {
                                          mainController
                                              .deleteDriverNotCompleteRegistration(
                                                  driver['phone_number']);
                                        },
                                      ),
                                    ],
                                  ),

                                  const SizedBox(height: 16),

                                  // Notes box
                                  CupertinoTextField(
                                    controller: notesController,
                                    placeholder: "Additional comments".tr,
                                    padding: const EdgeInsets.all(12),
                                    decoration: BoxDecoration(
                                      color: Colors.grey.shade100,
                                      borderRadius: BorderRadius.circular(8),
                                    ),
                                    maxLines: 2,
                                  ),

                                  const SizedBox(height: 12),

                                  // Save button
                                  Align(
                                    alignment: AlignmentDirectional.centerEnd,
                                    child: CupertinoButton(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 20),
                                      color: AppColor.secondaryColor,
                                      onPressed: () {
                                        mainController
                                            .saveNoteForDriverNotCompleteRegistration(
                                          driver['phone_number'],
                                          box.read(BoxName.employeename) ??
                                              'none',
                                          notesController.text,
                                        );
                                      },
                                      child: Text('Save Notes'.tr),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      ),
              ),
            ],
          );
        }),
      ],
    );
  }
}
