<?php

namespace App\Services;

class MobileNormalizationService
{
    public static function normalize(string $mobile): array
    {
        // Strip out non-numeric characters except leading +
        $cleaned = preg_replace('/[^\d]/', '', $mobile);

        if (str_starts_with($cleaned, '0')) {
            // Convert 0501234567 to 971501234567
            $normalized = '971'.substr($cleaned, 1);
        } elseif (str_starts_with($cleaned, '971')) {
            $normalized = $cleaned;
        } else {
            $normalized = '971'.$cleaned;
        }

        $nationalNumber = substr($normalized, 3);
        $display = '+971 '.substr($nationalNumber, 0, 2).' '.substr($nationalNumber, 2, 3).' '.substr($nationalNumber, 5);

        return [
            'country_code' => '971',
            'national_number' => $nationalNumber,
            'normalized_mobile' => $normalized,
            'display_mobile' => $display,
        ];
    }
}
