<?php

use Illuminate\Support\Str;

if (! function_exists('generateReferralCode')) {
    /**
     * Generate referral code from username and unique number
     */
    function generateReferralCode(string $username, int $unique): string
    {
        $prefix = Str::substr($username, 0, 6);
        $year = date('y');

        return Str::upper($prefix).$year.$unique;
    }
}

if (! function_exists('convertNumberToIndonesianWords')) {
    function convertNumberToIndonesianWords($angka)
    {
        $angka = (int) abs($angka);
        $huruf = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan', 'sepuluh', 'sebelas'];

        if ($angka < 12) {
            return $huruf[$angka];
        } elseif ($angka < 20) {
            return $huruf[$angka - 10].' belas';
        } elseif ($angka < 100) {
            $result = $huruf[$angka / 10].' puluh';
            $sisa = $angka % 10;
            if ($sisa > 0) {
                $result .= ' '.$huruf[$sisa];
            }

            return $result;
        } elseif ($angka < 200) {
            return 'seratus '.convertNumberToIndonesianWords($angka - 100);
        } elseif ($angka < 1000) {
            $result = $huruf[$angka / 100].' ratus';
            $sisa = $angka % 100;
            if ($sisa > 0) {
                $result .= ' '.convertNumberToIndonesianWords($sisa);
            }

            return $result;
        } elseif ($angka < 2000) {
            return 'seribu '.convertNumberToIndonesianWords($angka - 1000);
        } elseif ($angka < 1000000) {
            $result = convertNumberToIndonesianWords($angka / 1000).' ribu';
            $sisa = $angka % 1000;
            if ($sisa > 0) {
                $result .= ' '.convertNumberToIndonesianWords($sisa);
            }

            return $result;
        } elseif ($angka < 1000000000) {
            $result = convertNumberToIndonesianWords($angka / 1000000).' juta';
            $sisa = $angka % 1000000;
            if ($sisa > 0) {
                $result .= ' '.convertNumberToIndonesianWords($sisa);
            }

            return $result;
        } elseif ($angka < 1000000000000) {
            $result = convertNumberToIndonesianWords($angka / 1000000000).' milyar';
            $sisa = $angka % 1000000000;
            if ($sisa > 0) {
                $result .= ' '.convertNumberToIndonesianWords($sisa);
            }

            return $result;
        } elseif ($angka < 1000000000000000) {
            $result = convertNumberToIndonesianWords($angka / 1000000000000).' trilyun';
            $sisa = $angka % 1000000000000;
            if ($sisa > 0) {
                $result .= ' '.convertNumberToIndonesianWords($sisa);
            }

            return $result;
        } else {
            return 'Angka terlalu besar';
        }
    }
}

if (! function_exists('formatToWhatsApp')) {
    function formatToWhatsApp(string $phone): ?string
    {
        // Remove spaces, dashes, parentheses
        $cleaned = preg_replace('/[\s\-\(\)]/', '', $phone);

        if ($cleaned === null || $cleaned === '') {
            return null;
        }

        // Remove leading +
        if (str_starts_with($cleaned, '+62')) {
            $cleaned = substr($cleaned, 1);
        }

        // Replace 08 with 62
        if (str_starts_with($cleaned, '08')) {
            $cleaned = '62'.substr($cleaned, 1);
        }

        // If starts with 8, assume Indonesia
        if (str_starts_with($cleaned, '8')) {
            $cleaned = '62'.$cleaned;
        }

        // Final validation
        if (! str_starts_with($cleaned, '62')) {
            return null;
        }

        // Indonesia phone length sanity check
        if (strlen($cleaned) < 11 || strlen($cleaned) > 15) {
            return null;
        }

        return $cleaned;
    }
}
