<?php

namespace BWDK\Support;

if (!defined('ABSPATH')) { exit; }

class PhoneHelper
{
    /**
     * ورودی: هر فرمتی از موبایل (با فاصله/خط تیره/صفر/+98/…)
     * خروجی: E.164 بدون +  (مثلاً: 989121234567) یا '' اگر نامعتبر
     */
    public static function normalize_to_e164_no_plus(string $raw): string
    {
        $p = trim($raw);
        $p = preg_replace('/[\s\-\(\)]/', '', $p ?? '');
        if ($p === '') return '';

        // +98 -> 98
        if (strpos($p, '+98') === 0) {
            $p = '98' . substr($p, 3);
        }

        // 09xxxxxxxxx -> 98 9xxxxxxxxx
        if (preg_match('/^09\d{9}$/', $p)) {
            $p = '98' . substr($p, 1); // remove leading 0
        }

        // 9xxxxxxxxx (10 digits starting with 9) -> 989xxxxxxxxx
        if (preg_match('/^9\d{9}$/', $p)) {
            $p = '98' . $p;
        }

        // already like 989xxxxxxxxx
        if (!preg_match('/^98\d{10}$/', $p)) {
            return ''; // invalid for Iran
        }
        return $p;
    }

    /**
     * نمایش/ذخیره به فرمت دلخواه سایت
     * $format: with_zero | no_zero | country_plus | country_no_plus
     */
    public static function format_for_site(string $e164, string $format): string
    {
        if (!preg_match('/^98\d{10}$/', $e164)) return '';
        $local = substr($e164, 2); // 9xxxxxxxxx (10 digits)
        switch ($format) {
            case 'with_zero':
                return '0' . $local;            // 09xxxxxxxxx
            case 'no_zero':
                return $local;                   // 9xxxxxxxxx
            case 'country_plus':
                return '+' . $e164;               // +989xxxxxxxxx
            case 'country_no_plus': // fallthrough
            default:
                return $e164;                    // 989xxxxxxxxx
        }
    }

    /**
     * Format username based on selected format
     * Returns username string based on phone and format selection
     * 
     * @param string $e164 Phone in E.164 format without + (e.g. 989190327468)
     * @param string $format_key Format key: with_zero | no_zero | country_plus | country_no_plus
     * @return string Formatted username
     */
    public static function format_username(string $e164, string $format_key): string
    {
        if (!preg_match('/^98\d{10}$/', $e164)) {
            return '';
        }
        
        $formatted_phone = self::format_for_site($e164, $format_key);
        
        // Remove any special characters that might not be valid in usernames
        // Keep only alphanumeric characters
        $username = preg_replace('/[^a-zA-Z0-9]/', '', $formatted_phone);
        
        return $username;
    }

    /**
     * Format email address: <phone>@<site-domain>
     * Always uses E.164 format (no special characters) for email
     * 
     * @param string $e164 Phone in E.164 format without + (e.g. 989190327468)
     * @return string Email address
     */
    public static function format_email(string $e164): string
    {
        if (!preg_match('/^98\d{10}$/', $e164)) {
            return '';
        }
        
        // Get site domain from home_url
        $home_url = home_url();
        $parsed = wp_parse_url($home_url);
        $domain = $parsed['host'] ?? 'no-mail.local';
        
        // Remove www. prefix if present
        $domain = preg_replace('/^www\./', '', $domain);
        
        return $e164 . '@' . $domain;
    }

    /**
     * Get site domain from home_url (without www prefix)
     * 
     * @return string Site domain
     */
    public static function get_site_domain(): string
    {
        $home_url = home_url();
        $parsed = wp_parse_url($home_url);
        $domain = $parsed['host'] ?? 'no-mail.local';
        
        // Remove www. prefix if present
        $domain = preg_replace('/^www\./', '', $domain);
        
        return $domain;
    }
}



