/**
 * Validate and normalize a phone number, and detect telecom using DB prefixes.
 *
 * @param string $number
 * @return array{isValid: bool, telecomId?: int|null, normalized?: string}
 */
// function check_number_validity($number)
// {
//     // Clean to digits only
//     $number = preg_replace('/[^0-9]/', '', $number);

//     // Normalize formats
//     if (preg_match('/^0\d{9}$/', $number)) {
//         $number = '256' . substr($number, 1);
//     } elseif (preg_match('/^\d{9}$/', $number)) {
//         $number = '256' . $number;
//     }

//     // Must be 256xxxxxxxxx
//     if (! preg_match('/^256\d{9}$/', $number)) {
//         return ['isValid' => false];
//     }

//     // Check against telecom prefixes
//     $telecoms = Telecom::all();
//     foreach ($telecoms as $telecom) {
//         $prefixes = explode(',', $telecom->prefixes);
//         foreach ($prefixes as $prefix) {
//             $prefix = trim($prefix);
//             if (strpos($number, $prefix) === 0) {
//                 return [
//                     'isValid'    => true,
//                     'telecomId'  => $telecom->telecom_id,
//                     'normalized' => $number,
//                 ];
//             }
//         }
//     }

//     return ['isValid' => false];
// }

/**
 * Categorize phone numbers by telecom using their prefixes.
 *
 * @param array $contacts Array of phone numbers (e.g. ['256772000000', '256752000000'])
 * @return array Associative array grouped by telecom_id: [telecom_id => [numbers...]]
 */
// function categorizeContactsByTelecom(array $contacts)
// {
//     $allTelecoms = Telecom::all();
//     $map         = [];

//     foreach ($contacts as $contact) {
//         // Clean contact (remove spaces or + if any)
//         $contact = preg_replace('/[^0-9]/', '', $contact);

//         foreach ($allTelecoms as $telecom) {
//             $prefixes = explode(',', $telecom->prefixes);

//             foreach ($prefixes as $prefix) {
//                 $prefix = trim($prefix);
//                 if (strpos($contact, $prefix) === 0) {
//                     $map[$telecom->telecom_id][] = $contact;
//                     break 2; // stop checking other telecoms once matched
//                 }
//             }
//         }
//     }

//     return $map;
// }

/**
 * Summary of check_array_number_validity
 * @param mixed $contacts
 * @return array{filteredContacts: array, filteredOutContacts: array}
 */
// function check_array_number_validity ($contacts) {
//     $modifiedNumbers = array_map(fn ($number) => ltrim(str_replace('+', '', $number), '0'), $contacts);

//     $modifiedNumbers = array_map(fn ($number) => strpos($number, '256') !== 0 ? '256' . $number : $number, $modifiedNumbers);

//     $filteredContacts = array_filter($modifiedNumbers, function ($number) {
//         $length = strlen($number);
//         return $length === 12;
//     });

//     $filteredOutContacts = array_diff($modifiedNumbers, $filteredContacts);
//     $returnedContacts = ["filteredContacts" => $filteredContacts, "filteredOutContacts" => $filteredOutContacts];

//     return $returnedContacts;

// }