// // ignore_for_file: avoid_print
// import 'dart:io';
// import 'package:flutter/material.dart';
// import 'package:send_message/send_message.dart' show sendSMS;
// import 'package:simcards/sim_card.dart';
// import 'package:simcards/simcards.dart';

// import 'package:uuid/uuid.dart';

// class SmsService {
//   final Simcards _simcards = Simcards();

//  Future<void> sendVerificationSms({
//    required BuildContext context,
//    required String destinationNumber,
//    required String message,
//  }) async {
//    try {
//      await _simcards.requestPermission();

//      bool permissionGranted = await _simcards.hasPermission();
//      if (!permissionGranted) {
//        print("Permission denied." );
//        return;
//      }

//      List<SimCard> simCardList = await _simcards.getSimCards();
//      if (simCardList.isEmpty) {
//        print("No SIM detected." );
//        return;
//      }

//      await _sendSms(destinationNumber, message, simCardList.first);

//    } catch (e) {
//      print("Error in SMS process: $e");
//    }
//  }


//    Future<void> _sendSms(
//        String destinationNumber, String message, SimCard selectedSim) async {
//      if (Platform.isAndroid) {
//        try {
//          var uuid = const Uuid();
//          String uniqueId = uuid.v4(); 

//          String smsMessage = uniqueId;
//          String result = await sendSMS(
//            message: smsMessage,
//            recipients: [destinationNumber],
//            sendDirect: false,
//          );
//          print("SMS send result:  $result. Sent via ${selectedSim.displayName} (Note: OS default SIM isused).");

//        } catch (e) {
//          print("Error sending SMS: $e");
//        }
//      } else {
//        print("SMS sending is only supported on Android.");
//      }
//    }
// }

//  import 'dart:io';
//  import 'package:flutter/material.dart';
//  import 'package:permission_handler/permission_handler.dart'; // Import permission_handler
//  import 'package:send_message/send_message.dart' show sendSMS;
//  import 'package:simcards/sim_card.dart';
//  import 'package:simcards/simcards.dart';

//  class SmsService {
//    final Simcards _simcards = Simcards();

//    Future<bool> sendVerificationSms({
//      required BuildContext context,
//      required String destinationNumber,
//      required String message,
//    }) async {
//      try {
//        // --- NEW PERMISSION LOGIC ---
//        // 1. Request both Phone and SMS permissions
//        Map<Permission, PermissionStatus> statuses = await [
//          Permission.phone,
//          Permission.sms,
//        ].request();

//        // 2. Check if both permissions were granted
//        if (statuses[Permission.phone]!.isGranted && statuses[Permission.sms]!.isGranted) {
//          print("Phone and SMS permissions are granted.");
//        } else {
//          print("Permission was denied. Phone status: ${statuses[Permission.phone]}, SMS status: ${statuses[Permission.sms]}");
//          // Optionally, you can open app settings to let the user grant it manually
//          // openAppSettings();
//          return false;
//        }
//        // --- END OF NEW PERMISSION LOGIC ---


//        // Check for SIM card (this part remains the same)
//        List<SimCard> simCardList = await _simcards.getSimCards();
//        if (simCardList.isEmpty) {
//          print("No SIM card detected.");
//          return false;
//        }

//        // Try sending the SMS and return the result
//        return await _sendSms(destinationNumber, message, simCardList.first);

//      } catch (e) {
//        print("An error occurred in the SMS process: $e");
//        return false;
//      }
//    }

//    Future<bool> _sendSms(
//        String destinationNumber, String message, SimCard selectedSim) async {
//      if (Platform.isAndroid) {
//        try {
//          String smsMessage = message;
//          String result = await sendSMS(
//            message: smsMessage,
//            recipients: [destinationNumber],
//            sendDirect: true, // Still attempting direct send as requested
//          );
//          print("Background SMS send attempt result: $result");

//          if (result.toLowerCase().contains('sent')) {
//            print("Success: SMS appears to have been sent.");
//            return true;
//          } else {
//            print("Failure: SMS was not sent. Result: $result");
//            return false;
//          }
//        } catch (e) {
//          print("Error attempting to send SMS directly: $e");
//          return false;
//        }
//      } else {
//        print("SMS sending is only supported on Android.");
//        return false;
//      }
//    }
//  }

 import 'dart:io';
 import 'package:flutter/material.dart';
 import 'package:permission_handler/permission_handler.dart';
 import 'package:send_message/send_message.dart' show sendSMS;
 import 'package:simcards/sim_card.dart';
 import 'package:simcards/simcards.dart';

 // This enum provides detailed status back to the UI layer.
 enum PermissionStatusResult { granted, denied, permanentlyDenied, restricted }

 class SmsService {
   final Simcards _simcards = Simcards();

   /// Handles the requesting of SMS and Phone permissions.
   /// Returns a detailed status: granted, denied, or permanentlyDenied.
     Future<PermissionStatusResult> handleSmsPermission() async {
   var smsStatus = await Permission.sms.status;
   var phoneStatus = await Permission.phone.status;

   // Check initial status
   if (smsStatus.isGranted && phoneStatus.isGranted) {
     return PermissionStatusResult.granted;
   }
   if (smsStatus.isPermanentlyDenied || phoneStatus.isPermanentlyDenied) {
     return PermissionStatusResult.permanentlyDenied;
   }
   if (smsStatus.isRestricted || phoneStatus.isRestricted) {
     return PermissionStatusResult.restricted;
   }

   // Request permissions if not granted
   print("Requesting SMS and Phone permissions...");
   await [Permission.phone, Permission.sms].request();

   // Re-check status after request
   smsStatus = await Permission.sms.status;
   phoneStatus = await Permission.phone.status;

   if (smsStatus.isGranted && phoneStatus.isGranted) {
     return PermissionStatusResult.granted;
   }
   if (smsStatus.isPermanentlyDenied || phoneStatus.isPermanentlyDenied) {
     return PermissionStatusResult.permanentlyDenied;
   }
   if (smsStatus.isRestricted || phoneStatus.isRestricted) {
     return PermissionStatusResult.restricted;
   }

   // If none of the above, it's denied
   return PermissionStatusResult.denied;
 }

   /// Tries to send a single verification SMS.
   /// This should only be called AFTER permissions have been granted.
   Future<bool> sendVerificationSms({
     required BuildContext context,
     required String destinationNumber,
     required String message,
   }) async {
     try {
       List<SimCard> simCardList = await _simcards.getSimCards();
       if (simCardList.isEmpty) {
         print("No SIM card detected.");
         return false;
       }
       return await _sendSms(destinationNumber, message, simCardList.first);
     } catch (e) {
       print("An error occurred in the SMS process: $e");
       return false;
     }
   }

   /// Private function to perform the SMS sending action.
   Future<bool> _sendSms(
       String destinationNumber, String message, SimCard selectedSim) async {
     if (Platform.isAndroid) {
       try {
         String smsMessage = message;
         String result = await sendSMS(
           message: smsMessage,
           recipients: [destinationNumber],
           sendDirect: true,
         );
         print("Background SMS send attempt result: $result");

         if (result.toLowerCase().contains('sent')) {
           print("Success: SMS appears to have been sent.");
           return true;
         } else {
           print("Failure: SMS was not sent. Result: $result");
           return false;
         }
       } catch (e) {
         print("Error attempting to send SMS directly: $e");
         return false;
       }
     } else {
       print("SMS sending is only supported on Android.");
       return false;
     }
   }
 }
