<?php defined('BASEPATH') OR exit('No direct script access allowed');


function number_format_pakistani($num)
{
    $explrestunits = "" ;
    if(strlen($num)>3){
        $lastthree = substr($num, strlen($num)-3, strlen($num));
        $restunits = substr($num, 0, strlen($num)-3); // first digits
        $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits;
        $expunit = str_split($restunits, 2);

        foreach($expunit as $i => $unit){
            if($i == 0){
                $explrestunits .= (int)$unit.",";
            }else{
                $explrestunits .= $unit.",";
            }
        }
        return $explrestunits.$lastthree;
    } else {
        return $num;
    }
}
function amount_format_pakistani($num)
{
    $num = number_format((float)$num, 2, '.', '');
    $parts = explode('.', $num);
    $integer = $parts[0];
    $decimal = isset($parts[1]) ? $parts[1] : '00';

    $explrestunits = "";

    if (strlen($integer) > 3) {
        $lastthree = substr($integer, -3);
        $restunits = substr($integer, 0, -3);
        $restunits = (strlen($restunits) % 2 == 1) ? "0" . $restunits : $restunits;
        $expunit = str_split($restunits, 2);

        foreach ($expunit as $i => $unit) {
            if ($i == 0) {
                $explrestunits .= (int)$unit . ",";
            } else {
                $explrestunits .= $unit . ",";
            }
        }
        $formatted = $explrestunits . $lastthree;
    } else {
        $formatted = $integer;
    }

    // Remove .00 if decimal is zero
    if ($decimal === "00") {
        return "Rs. " . $formatted;
    } else {
        return "Rs. " . $formatted . "." . $decimal;
    }
}


function phone_number_format_pakistani($number) {
    // Remove all non-digit characters
    $number = preg_replace("/[^\d]/", "", $number);

    // If number starts with 92 and is 12 digits: convert to 03
    if (strlen($number) == 12 && substr($number, 0, 2) == "92") {
        $number = "0" . substr($number, 2);
    }

    // Format if it's now 11 digits
    if (strlen($number) == 11) {
        return preg_replace("/^(\d{4})(\d{4})(\d{3})$/", "$1-$2-$3", $number);
    }

    return $number; // fallback
}


function cnic_format_pakistani($cnic) {
    // Remove non-digits just in case
    $cnic = preg_replace("/[^\d]/", "", $cnic);

    // Match and format if it's 13 digits
    if (strlen($cnic) === 13) {
        return preg_replace("/^(\d{5})(\d{7})(\d{1})$/", "$1-$2-$3", $cnic);
    }

    // Return original if not valid
    return $cnic;
}


if (!function_exists('date_format_month_year_01')) {
    function date_format_month_year_01($month, $year, $full_name = false)
    {
        // If month is string like "Feb" or "February", convert to number
        if (!is_numeric($month)) {
            $timestamp = strtotime("1 $month $year");
            $month_num = date('m', $timestamp);
        } else {
            $month_num = str_pad($month, 2, '0', STR_PAD_LEFT);
        }

        $format = $full_name ? 'F Y' : 'M Y'; // F = February, M = Feb
        return DateTime::createFromFormat('!m Y', "$month_num $year")->format($format);
    }
}



function get_browser_info()
{
    $CI =& get_instance();
    $CI->load->library('user_agent');

    $browser = $CI->agent->browser() . ' ' . $CI->agent->version();
    $os = $CI->agent->platform();
    $device = $CI->agent->is_mobile() ? $CI->agent->mobile() : 'Desktop';

    return [
        'browser' => $browser,
        'os' => $os,
        'device' => $device
    ];
}


?>